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

标签 android colors android-linearlayout

为了让我的应用程序的用户知道当前哪个字段具有焦点,我正在尝试根据当前状态更改某些字段的背景颜色,但是,我在理解 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 和 background 的属性值,我得到一个异常,说明

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

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

最佳答案

我遇到了这个确切的问题。在我看来, 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 来引用此可绘制对象。

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

关于android - 如何在颜色状态列表资源中指定背景颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3953606/

相关文章:

Android RelativeLayout 和 OnClickListener

Android Studio 2.2.3 找不到 <vector>

android - 从特定于 Xamarin.Forms 中的共享项目的平台中的自定义 View 呈现器获取数据

java - ArrayList<User> 好友。 VkAPI

R:shapefile 上的梯度图

powershell - Powershell 输出的自定义字体颜色

C 如何改变单词的颜色

android - 带有可拖动芯片的芯片组

android - 如何在 Android 中使用位图作为背景从 LinearLayout 中删除黑条?

android - 不同屏幕上的布局问题