Android的定制按钮;改变文字颜色按钮、颜色、文字、Android

2023-09-11 12:51:21 作者:︶帅到失眠

我做了一个按钮,改变不同的国家背景绘制,这种方式:

I made a button that changes the background drawable on different states, this way:

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
 <item android:state_pressed="true" android:drawable="@drawable/btn_location_pressed" /> <!-- pressed -->
 <item android:state_focused="true" android:drawable="@drawable/btn_location_pressed"/> <!-- focused -->
 <item android:drawable="@drawable/btn_location"/> <!-- default -->

这里的问题是,我也想改变文字颜色为我做的绘制,但我不能够。我已经尝试过机器人:文字颜色和android:颜色,但先不工作,而秒改变我的背景

The problem here is that I'm also trying to change the textColor as I do with the drawable but I'm not being able to. I already tried android:textColor and android:color but the first doesn't work whilst the seconds changes my background.

接下来的code是我的布局的一部分。对于文本颜色它仅适用于正常状态文本颜色,因此不能将其改为了白色的,而pressed

The next code is part of my layout. Regarding to the text color it only works for the normal state text color, thus not changing it to the white one while pressed

<Button android:id="@+id/location_name"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:paddingTop="5dp"
        android:background="@drawable/location"          
        android:textSize="15sp"
        android:textColor="@color/location_color"
        android:textColorHighlight="#FFFFFF"
   />

有没有人有一个线索?

Has anybody got a clue?

TY! :)

推荐答案

为您的按钮,创建一个有状态的颜色,就像你的背景,例如:

Create a stateful color for your button, just like you did for background, for example:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_focused="true" android:state_pressed="false" android:color="#ffffff" />
    <item android:state_focused="true" android:state_pressed="true" android:color="#000000" />
    <item android:state_focused="false" android:state_pressed="true" android:color="#000000" />
    <item android:color="#ffffff" />
</selector>

将XML文件中的,在RES /绘制文件夹,即RES /绘制/ button_text_color.xml。然后,只需设置绘制文本颜色:

Place the xml in a file at res/drawable folder i.e. res/drawable/button_text_color.xml. Then just set the drawable as text color:

android:textColor="@drawable/button_text_color"