【发布时间】:2015-10-21 07:56:37
【问题描述】:
我正在开发一个支持 Jelly Beans 的 Android 应用程序。我有自己的风格定义为:
<style name="AppTheme" parent="Theme.AppCompat.NoActionBar">
</style>
<style name="AppTheme.Child" parent="AppTheme">
<item name="colorButtonNormal">@color/login_button_enabled</item>
</style>
<style name="HomeButton" parent="Widget.AppCompat.Button">
<item name="android:background">@drawable/home_button</item>
</style>
而drawable/home_button.xml定义为:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<inset xmlns:android="http://schemas.android.com/apk/res/android"
android:insetLeft="@dimen/button_inset_horizontal_material"
android:insetTop="@dimen/button_inset_vertical_material"
android:insetRight="@dimen/button_inset_horizontal_material"
android:insetBottom="@dimen/button_inset_vertical_material">
<shape android:shape="rectangle">
<corners android:radius="@dimen/control_corner_material" />
<solid android:color="?attr/colorButtonNormal" />
<padding android:left="@dimen/button_padding_horizontal_material"
android:top="@dimen/button_padding_vertical_material"
android:right="@dimen/button_padding_horizontal_material"
android:bottom="@dimen/button_padding_vertical_material" />
</shape>
</inset>
</item>
</selector>
这种风格在 Lollipop+ 中完美运行,但是对于棒棒糖之前的 Android,如果我添加 style="@style/home_button" 它只会崩溃。 logcat 显示错误为:
java.lang.RuntimeException: Unable to start activity ComponentInfo{my.domain.app/my.domain.app.MyActivity}: android.view.InflateException: Binary XML file line #46: Error inflating class Button
...
Caused by: android.view.InflateException: Binary XML file line #46: Error inflating class Button
...
Caused by: android.content.res.Resources$NotFoundException: File res/drawable/home_button.xml from drawable resource ID #0x...
...
Caused by: java.lang.UnsupportedOperationException: Can't convert to color: type=0x2
如果我删除<solid android:color="?attr/colorButtonNormal" /> 行,应用程序不会崩溃,但不会呈现颜色。 我想我的风格有问题,但我不知道是什么。我的 build.gradle 中的构建配置和库是:
apply plugin: 'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "xx.xxxx.xxx"
minSdkVersion 14
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
debug {
testCoverageEnabled true
}
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(include: ['*.jar'], dir: 'libs')
compile 'com.android.support:appcompat-v7:22.2.1'
compile 'com.android.support:design:22.2.1'
compile 'com.android.support:cardview-v7:22.2.1'
compile project(':xxxxx')
}
感谢您的帮助!
【问题讨论】:
-
您可能已经检查过了,但是...您是否检查了 Android Manifest 中的 android:theme="@style/Theme.AppCompat.Light" 其次,我们可以看到带有导入的活动吗?
标签: java android button styles android-appcompat