如何在颜色状态列表资源中指定背景颜色?

问题描述 投票:17回答:3

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

我找到了例子(对不起,URL不再有用),如果我尝试完全相同,即如果我想改编textColor,事情确实有效。但是,如果我尝试一个稍微不同的东西,即适应背景颜色,事情不起作用,我不明白为什么?为什么这么不一致?

为了更容易理解我想要做什么,我追加我的misc。 .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和background的属性值我得到一个例外,说明

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

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

android colors android-linearlayout
3个回答
0
投票

尝试将textColor定义为drawable,而不是颜色:

android:textColor="@drawable/button_test_color"

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


55
投票

我有这个确切的问题。它看起来像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,需要上面的State List Drawable方法。


0
投票

如果您已经有了颜色状态列表,那么您可以创建一个这样的简单绘图并使用它。

res/drawable/button_test_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <solid android:color="@color/button_test_color" />
</shape>

<Button
    ...
    android:background="@drawable/button_test_background.xml"
    ...
 />
© www.soinside.com 2019 - 2024. All rights reserved.