为什么android不能识别颜色状态列表文件?

时间:2013-04-12 16:00:59

标签: android colors android-drawable

我想要一个可以在按下时突出显示的文本按钮,它将具有空白背景。 (基本上就像浏览器中的链接一样)

我用了一个按钮。

以下代码适用于我为“background”属性输入的代码。如果我输入textColor属性,我会得到“android.content.res.Resources $ NotFoundException:”的“textbutton”资源(我试过将它添加到color和drawble目录中)

<Button android:text="My Text" android:background="@drawable/textbutton" android:textColor="#FFFFFFFF" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>

以下代码不起作用:

<Button android:text="My Text" android:background="#0000" android:textColor="@drawable/textbutton" android:layout_height="wrap_content" android:layout_width="wrap_content"></Button>

(使它成为一种颜色/,而不是一种可绘制的颜色也不起作用)

这是textbutton.xml的内容:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"><color android:color="#FFFFFFFF"></color></item>
    <item android:state_focused="true"><color android:color="#FFFFFFFF"></color></item>
    <item><color android:color="#FF33b5e5"></color></item>
</selector>

2 个答案:

答案 0 :(得分:2)

问题是没有在“item”标签内定义“color”属性。

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:color="#FFFFFFFF"></item>
    <item android:state_focused="true" android:color="#FFFFFFFF"></item>
    <item android:color="#FF33b5e5"></item>
</selector>

答案 1 :(得分:1)

<?xml version="1.0" encoding="utf-8"?>

<color name="md__transparent">#00000000</color>
<color name="md__defaultBackground">#FF555555</color>
<color name="gesture_color">#FFFFFF00</color>
<color name="background_color">#2F4F92</color>
<color name="background_grey_color">#D8D8D8</color>
<color name="header_background_color">#FFAE02</color>
<color name="list_selection_dark">#a3a6a8</color>
<color name="tabBackground">#2568ba</color>
<color name="tabMedium">#1c4c80</color>
<color name="tabDark">#1d66ab</color>
<color name="tabTransparent">#10ffffff</color>
<color name="tabHeader">#FF4000</color>
<color name="tabUnselected">#cfcfcf</color>
<color name="title_background">#D7D4D4</color>
<color name="title_text">#000000</color>
<color name="title_text_alt">#ffffffff</color>
<color name="foreground1">#ff355689</color>
<color name="foreground2">#ff7081a3</color>
<color name="background1">#ffffffff</color>
<color name="background2">#ffd5ddeb</color>
<color name="background3">#ffe3e8f1</color>
<color name="title_separator">#40ffffff</color>
<color name="contents_text">#ff000000</color>
<color name="encode_view">#ffffffff</color>
<color name="grey">#D7D4D4</color>
<color name="help_button_view">#ffcccccc</color>
<color name="help_view">#ff404040</color>
<color name="possible_result_points">#c0ffff00</color>
<color name="result_image_border">#ffffffff</color>
<color name="result_minor_text">#ffc0c0c0</color>
<color name="result_points">#c000ff00</color>
<color name="result_text">#ffffffff</color>
<color name="result_view">#b0000000</color>
<color name="sbc_header_text">#ff808080</color>
<color name="sbc_header_view">#ffffffff</color>
<color name="sbc_list_item">#fffff0e0</color>
<color name="sbc_layout_view">#ffffffff</color>
<color name="sbc_page_number_text">#ff000000</color>
<color name="sbc_snippet_text">#ff4b4b4b</color>
<color name="share_text">#ff000000</color>
<color name="status_view">#50000000</color>
<color name="status_text">#ffffffff</color>
<color name="transparent">#00000000</color>
<color name="viewfinder_frame">#ff000000</color>
<color name="viewfinder_laser">#ffff0000</color>
<color name="viewfinder_mask">#60000000</color>
<color name="accent_1">#ff29549f</color>

<drawable name="white">#FFFFFF</drawable>
<drawable name="android_orange">#FF9E18</drawable>
<drawable name="android_yellow">#F2E406</drawable>

<color name="blue">#770000ff</color>
<color name="green">#7700ff00</color>
<color name="yellow">#77ffff00</color>
<color name="red">#FF0000</color>
<color name="screen_background_black">#ff000000</color>
<color name="translucent_background">#e0000000</color>
<color name="transparent_background">#00000000</color>
<color name="solid_red">#FF0000</color>
<color name="solid_blue">#0000ff</color>
<color name="solid_green">#f0f0</color>
<color name="solid_yellow">#ffffff00</color>
<color name="White">#FFFFFF</color>
<color name="Black">#000000</color>
<color name="actionbar_background_light">#ccffffff</color>
<color name="actionbar_background_dark">#cc000000</color>
<color name="drag_active_color">#80cccccc</color>
<color name="actionbar_text">#ff29549f</color>

将此xml保存为项目的values文件夹中的“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="@color/Black>
    </item>

    <item
        android:state_focused="true"
        android:color="@color/Blue>
    </item>

    <item android:color="@color/White">
    </item>
</selector>

这是更有效的使用方式,因为它可以在任何布局中用于文本颜色,背景等。