本文介绍了对于棒棒糖前设备使用attr的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

并且drawable / home_button.xml定义为:

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>

这种风格在Lollipop +中完美无缺,但对于pre-lollipop Android,如果我添加style =@ style / home_button它只是崩溃了。 logcat显示错误:

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

如果我删除行< solid android:color =?attr / colorButtonNormal/> 该应用程序不会崩溃但颜色不会呈现。 我想我的风格有问题,但我无法意识到。我的build.gradle中的构建配置和库是:

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

感谢您的帮助!

推荐答案

你没有做错 - drawables的主题属性(在你的情况下?attr / colorButtonNormal )在Lollipop之前的设备上不受支持。 (请参阅作为参考)

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)

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

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.

这篇关于对于棒棒糖前设备使用attr的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-17 02:51