Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android按钮文本颜色始终为粉红色_Android_Xml_Button_State - Fatal编程技术网

Android按钮文本颜色始终为粉红色

Android按钮文本颜色始终为粉红色,android,xml,button,state,Android,Xml,Button,State,当按钮在选定状态和非选定状态之间切换时,我尝试更改按钮文本颜色和按钮背景颜色。背景效果很好,但文本仅显示为粉红色(默认的colorPrimary,我已更改) res/drawable/map\u button\u background.xml <?xml version="1.0" encoding="utf-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <

当按钮在选定状态和非选定状态之间切换时,我尝试更改按钮文本颜色和按钮背景颜色。背景效果很好,但文本仅显示为粉红色(默认的
colorPrimary
,我已更改)

res/drawable/map\u button\u background.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="false">
        <shape android:shape="rectangle">
            <corners android:radius="4dp"/>
            <solid android:color="@android:color/transparent"/>
        </shape>
    </item>

    <item android:state_selected="true">
        <shape android:shape="rectangle">
            <corners android:radius="4dp"/>
            <solid android:color="@color/colorPrimary"/>
        </shape>
    </item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="false">
        <color android:color="@color/colorPrimary"/>
    </item>

    <item android:state_selected="true">
        <color android:color="@android:color/white"/>
    </item>
</selector>
<style name="Button.Map">
    <item name="android:layout_height">0dp</item>
    <item name="android:layout_weight">1</item>
    <item name="android:layout_margin">4dp</item>
    <item name="android:background">@drawable/map_button_background</item>
    <item name="android:textColor">@drawable/map_button_text</item>
</style>
res/styles/styles.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="false">
        <shape android:shape="rectangle">
            <corners android:radius="4dp"/>
            <solid android:color="@android:color/transparent"/>
        </shape>
    </item>

    <item android:state_selected="true">
        <shape android:shape="rectangle">
            <corners android:radius="4dp"/>
            <solid android:color="@color/colorPrimary"/>
        </shape>
    </item>
</selector>
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_selected="false">
        <color android:color="@color/colorPrimary"/>
    </item>

    <item android:state_selected="true">
        <color android:color="@android:color/white"/>
    </item>
</selector>
<style name="Button.Map">
    <item name="android:layout_height">0dp</item>
    <item name="android:layout_weight">1</item>
    <item name="android:layout_margin">4dp</item>
    <item name="android:background">@drawable/map_button_background</item>
    <item name="android:textColor">@drawable/map_button_text</item>
</style>

0dp
1.
4dp
@可绘制/地图按钮背景
@可绘制/地图按钮文本
此外,文本颜色从未改变,只是一直保持粉红色。我尝试添加
作为默认值,但仍然不起作用


你知道是什么引起的吗?

你有一些问题

首先,您的
元素应该直接具有
android:color
属性,而不是在子
元素中

其次,
android:textColor
需要引用颜色资源(或文字颜色值)

您已经将您的
map\u按钮\u文本
资源放置在您的
res/drawable
文件夹中,该文件夹告诉Android将其解释为可绘制的,而不是颜色

如果您将该文件移动到
res/color
并通过
@color/map\u button\u text
引用它,您应该会得到您想要的

最后,您还应该为选择器定义一个默认状态(一个没有任何
android:state\uu
属性的状态)

最终的XML应该如下所示:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/colorPrimary" android:state_selected="false"/>

    <!-- Default state -->
    <item android:color="@android:color/white"/>
</selector>

回答得真棒Bryan,我对此有一些问题,经过一些讨论后,我意识到这是因为它所在的目录。它位于
res/drawable
内部,而不是
res/color