如何在颜色状态列表资源中指定背景颜色? [英] How to specify background color in Color State List Resources?

查看:29
本文介绍了如何在颜色状态列表资源中指定背景颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

To give the user of my app an indication which field currently has the focus I am trying to change the background color of some of my fields depending on the current state, however, I am having troubles understanding Androids Color State List Resources:

我找到了示例(抱歉,URL 不再有效),如果我尝试完全相同,即如果我想调整 textColor ,事情 工作.但是,如果我只尝试稍微不同的事情,即调整背景颜色,事情工作,我不明白为什么?为什么这么不一致???

I found example (sorry, URL no longer works) and if I try exactly the same, i.e. if I want to adapt the textColor , things do work. However, if I try an only slightly different thing, namely to adapt the background color, things do not work and I don't understand why? Why is this so inconsistent???

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

To make it simpler to understand what I am trying to do, I append my misc. .xml files:

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> 

测试活动:

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>

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

If I run this as shown here, it works, i.e. I get a button whose text color changes depending on whether the button is focuses, pressed, etc.

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

If I uncomment the lower button, where I just flipped the attribute values for textColor and background I get an exception, stating

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

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

What the heck am I missing here? Why is that color state list acceptable as a text color but not as a background color? How does one specify a view's background color depending on the view's state?

推荐答案

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

Try defining textColor as a drawable, rather than a color:

android:textColor="@drawable/button_test_color"

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

Resource categories are based on what type of resources they are, not the name of the folder they are located in. XML files of the form button_test_color.xml are typically referenced as "drawable" - I'm actually surprised "color" worked at all!

这篇关于如何在颜色状态列表资源中指定背景颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆