无法解析颜色值颜色

2023-09-06 14:10:50 作者:时间不会说谎

我试图让我的按钮改变他们的文本的颜色,当它是pssed $ P $,但我跑成我不能解决的问题。我得到一个错误说无法解析颜色值,那么它​​给我的文件路径。这里是我的文件使用im

I am trying to make my buttons change the color of their text when it is pressed but i ran into a problem that i cant solve. I get an error saying "unable to resolve color value" then it gives me the path to the file. here are my files im using

这一个是在一个新的文件夹命名颜色的资源之下,其所谓的按钮

This one is in a new folder named color under resources and its called button

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

<Button android:text="Main Menu" android:textColor="@color/button"
    android:layout_width="200px" android:id="@+id/mainmenu"
    android:layout_height="55px" android:layout_x="5dip"  
    android:layout_y="174dip"
    android:textSize="18px">
 </Button>

其驾驶我疯了,如果有人可以帮助我。

its driving me crazy if anybody could help me.

推荐答案

我成功地做到了像这样的:

I successfully did it like this:

文件:

/drawable/button_states.xml
/layout/main.xml
/values/colors.xml

button_states.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/red" /> <!-- pressed -->
    <item android:state_focused="true"
        android:color="@color/blue_background" /> <!-- focused -->
    <item android:color="@color/white" /> <!-- default -->
</selector>

colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="blue_background">#FF2f74c3</color>
    <color name="white">#fefefe</color>
    <color name="red">#ff0000</color>
</resources>

layout.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_height="wrap_content"
    android:text="Button"
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:textColor="@drawable/button_states" />
</LinearLayout>
 
精彩推荐
图片推荐