对于棒棒糖前设备使用attr的正确方法是什么?

我正在开发一个支持Jelly Beans的Android应用程序。 我将自己的风格定义为:

   @color/login_button_enabled   @drawable/home_button  

drawable / home_button.xml定义为:

             

这种风格在Lollipop +中完美无缺,但对于pre-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 

如果我删除行 ,应用程序不会崩溃,但颜色不会呈现。 我想我的风格有问题,但我无法实现 。 我的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') } 

谢谢你的帮助!

你没有做错任何事 – 在Lollipop之前的设备上,不支持drawables的主题属性(在你的情况下是?attr/colorButtonNormal )。 (参见本期作为参考)

因此,要么直接引用颜色, 要么为要支持的每种颜色创建home_button.xml ,并将它们添加到稍后在活动代码中设置的不同主题。