如何颜色状态列出资源指定的背景颜色? [英] How to specify background color in Color State List Resources?
问题描述
为了让我的应用程序的指示该领域目前有我想改变我的一些根据当前状态字段的背景色的重点用户,但是,我有麻烦理解机器人颜色状态列出资源:
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不再工作),如果我尝试完全相同,也就是说,如果我要调整在文字颜色,事情的做的工作。但是,如果我尝试的唯一的略有不同的事情,也就是适应背景的颜色,事情的不要的工作,我不明白为什么?为什么会这样不一致的???
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 /价值/ 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 /彩色/ 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 /布局/ 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>
如果我运行这个如下图所示,它的工作原理,也就是我得到的文本取决于按钮是否被关注,pressed等颜色变化的按钮。
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.
如果我去掉下面的按钮,在这里我只是翻转的属性值文字颜色和背景我得到一个异常,说明
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?
推荐答案
尝试定义文字颜色为绘制,而不是一个颜色:
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屋!