264

我制作了一个按钮,可以通过这种方式更改不同状态下的背景可绘制对象:

<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 -->
</selector>

这里的问题是我也试图改变 textColor 就像我对 drawable 所做的那样,但我做不到。我已经尝试过 android:textColor 和 android:color 但是第一个不起作用,而秒数改变了我的背景。

下一个代码是我布局的一部分。关于文本颜色,它仅适用于正常状态的文本颜色,因此在按下时不会将其更改为白色

<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"
   />

有人有线索吗?

4

5 回答 5

594

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

<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Focused and not pressed -->
    <item android:state_focused="true" 
          android:state_pressed="false" 
          android:color="#ffffff" />

    <!-- Focused and pressed -->
    <item android:state_focused="true" 
          android:state_pressed="true" 
          android:color="#000000" />

    <!-- Unfocused and pressed -->
    <item android:state_focused="false" 
          android:state_pressed="true" 
          android:color="#000000" />

    <!-- Default color -->
    <item android:color="#ffffff" />

</selector>

将 xml 文件放在 res/drawable 文件夹的文件中,即 res/drawable/button_text_color.xml。然后只需将drawable设置为文本颜色:

android:textColor="@drawable/button_text_color"
于 2011-01-14T15:50:23.620 回答
18

另一种方法是在你的课堂上:

import android.graphics.Color; // add to top of class  

Button btn = (Button)findViewById(R.id.btn);

// set button text colour to be blue
btn.setTextColor(Color.parseColor("blue"));

// set button text colour to be red
btn.setTextColor(Color.parseColor("#FF0000"));

// set button text color to be a color from your resources (could be strings.xml)
btn.setTextColor(getResources().getColor(R.color.yourColor));

// set button background colour to be green
btn.setBackgroundColor(Color.GREEN);
于 2013-07-30T14:44:05.483 回答
5

好的,非常简单,首先转到 1. res-valuse 并打开 colors.xml 2. 复制 1 个已定义的文本,例如 #FF4081 并更改名称,例如我更改为白色并更改其值,例如我更改为 #FFFFFF像这样的白色值

<color name="White">#FFFFFF</color>

然后在你的按钮内添加这一行

 b3.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.White));

好的 b3 是我的按钮的名称,因此更改了按钮的名称,如果您使用白色,如果您更改不同的颜色,然后将白色更改为您的颜色名称,则所有其他按钮的名称都将相同,但首先您已经在颜色中定义了该颜色。 xml 就像我在 pont 2 中解释的那样

于 2017-06-28T05:35:12.800 回答
1

更改按钮的文本颜色

因为这个方法现在已经被弃用了

button.setTextColor(getResources().getColor(R.color.your_color));

我使用以下内容:

button.setTextColor(ContextCompat.getColor(mContext, R.color.your_color));
于 2016-08-24T12:11:43.417 回答
0

getColorStateList像这样使用

setTextColor(resources.getColorStateList(R.color.button_states_color))

代替getColor

setTextColor(resources.getColor(R.color.button_states_color))
于 2019-05-22T20:13:32.657 回答