What is the correct way to use attr for pre-lollipop devices?

大城市里の小女人 提交于 2019-12-22 18:37:34

问题


I am developing an Android application with support for Jelly Beans onwards. I have my own style defined as:

<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>

And drawable/home_button.xml is defined as:

<?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>

This style works flawlessly in Lollipop+, however for pre-lollipop Android, if I add style="@style/home_button" it just crashes. The logcat shows errors as:

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

If I remove the line <solid android:color="?attr/colorButtonNormal" /> the app does not crash but the color is not rendered. I suppose that I have something wrong with my style but I cannot realise what. My build configuration and libraries in my build.gradle are:

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')
}

Thanks for your help!


回答1:


You've done nothing wrong - theme attributes for drawables (in your case ?attr/colorButtonNormal) simply aren't supported on pre-Lollipop devices. (See this Issue as a reference)

So either reference the color directly or create a home_button.xml for every possible color you want to support and add them to different themes which you set in your activity code later on.



来源:https://stackoverflow.com/questions/31715645/what-is-the-correct-way-to-use-attr-for-pre-lollipop-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!