【问题标题】:How to specify background color in Color State List Resources?如何在颜色状态列表资源中指定背景颜色?
【发布时间】:2010-10-17 14:25:47
【问题描述】:

为了让我的应用程序的用户知道当前哪个字段具有焦点,我正在尝试根据当前状态更改某些字段的背景颜色,但是,我在理解 Android 的颜色状态列表资源时遇到了麻烦:

我找到了示例(抱歉,URL 不再有效),如果我尝试 exactly 相同,即如果我想调整 textColor ,事情 工作。但是,如果我只尝试略有不同的东西,即调整背景颜色,那么事情不起作用,我不明白为什么?为什么这么不一致???

为了更容易理解我想要做什么,我附上了我的杂项。 .xml 文件:

AndroidManifest.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="mmo.android.test"
      android:versionCode="1"
      android:versionName="1.0">
    <application android:icon="@drawable/icon" android:label="@string/app_name">
        <activity android:name=".Test"
                  android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>
</manifest> 

Test-Activity:

package mmo.android.test;

import android.app.Activity;
import android.os.Bundle;

public class Test extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

res/values/strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Test!</string>
    <string name="app_name">Test</string>
</resources>

res/color/button_test_color.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"   android:color="#f0f"/> <!-- pressed -->
    <item android:state_focused="true"   android:color="#ff0"/> <!-- focused -->
    <item android:color="#000"/> <!-- default -->
</selector>

最后是我的res/layout/main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="foobar"
        android:textColor="@color/button_test_color"
        android:background="#f00"
     />
    <!-- 
    <Button
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="foobar"
        android:textColor="#f00"
        android:background="@color/button_test_color"
     />
     -->
</LinearLayout>

如果我按此处所示运行它,它会起作用,即我得到一个按钮,其文本颜色会根据按钮是否聚焦、按下等而变化。

如果我取消注释下部按钮,我只是翻转了 textColor 和背景的属性值,我得到一个异常,说明

... <item> tag requires a 'drawable' attribute or child tag defining a drawable

我到底错过了什么?为什么该颜色状态列表可以作为文本颜色但不能作为背景颜色?如何根据视图的状态指定视图的背景颜色?

【问题讨论】:

    标签: android colors android-linearlayout


    【解决方案1】:

    我遇到了这个确切的问题。在我看来,android:background 不适用于颜色状态列表。我通过创建一个 State List Drawable 来解决这个问题(单个颜色可以用作 State List 中的 drawable)。

    要使用您的示例,请创建一个文件res/drawable/button_test_background.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true"   android:drawable="@color/pressed_color"/>
        <item android:state_focused="true"   android:drawable="@color/focused_color"/>
        <item android:drawable="@color/default_color"/>
    </selector>
    

    注意使用android:drawable 而不是android:color。 Android 将使用颜色资源并从中进行绘制。要完成此操作,您需要将颜色资源添加到您的 res/values/colors.xml 文件中:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        ...
        <color name="pressed_color">#f0f</color>
        <color name="focused_color">#ff0</color>
        <color name="default_color">#000</color>
        ...
    </resources>
    

    然后您将使用@drawable/button_test_background 而不是@color/button_test_color 来引用这个drawable。

    因此,总而言之,颜色状态列表对于android:textColor 工作正常,但对于android:background,则需要上述状态列表 Drawable 方法。

    【讨论】:

    • 真气。感谢您确认我的怀疑
    【解决方案2】:

    支持变体app:background 允许直接使用颜色状态列表作为背景。

    对于 Android 10+,android:background 也可以使用。

    【讨论】:

    • 这不起作用。它可以编译,但不会改变颜色。看来你必须在drawable中有状态列表
    【解决方案3】:

    尝试将 textColor 定义为可绘制对象,而不是颜色:

    android:textColor="@drawable/button_test_color"
    

    资源类别基于它们是什么类型的资源,而不是它们所在文件夹的名称。button_test_color.xml 形式的 XML 文件通常被称为“可绘制” - 我实际上很惊讶“颜色”完全有效!

    【讨论】:

    • 形成更多信息。关于使用可绘制资源,question has been asked before.
    • 我不知道为什么这个答案被标记为正确。提问者似乎遵循了Android docs,这表明@color 应该与 res/color 目录中的 xml 文件一起正常工作,并由文件名引用。
    • 我更惊讶的是,当它根本不起作用时,这是选择的答案。 -1
    • 无关紧要,不应成为公认的答案。 OP 使用 ColorStateList 的“背景”属性存在问题。
    猜你喜欢
    • 2019-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多