安卓定制按钮;改变文本颜色

时间:2022-11-04 21:53:37

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

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.

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

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?

有人知道吗?

4 个解决方案

#1


514  

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

    <!-- 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>

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:

将xml放在res/drawable文件夹(即res/drawable/button_text_color.xml)中的文件中。然后将可绘制文本设置为:

android:textColor="@drawable/button_text_color"

#2


16  

Another way to do it is in your class:

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

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);

#3


2  

ok very simple first go to 1. res-valuse and open colors.xml 2.copy 1 of the defined text their for example #FF4081 and change name for instance i changed to white and change its value for instance i changed to #FFFFFF for white value like this

很简单,先到1。res-valuse和开放的颜色。xml 2。在定义的文本中复制1,例如#FF4081和更改名称,例如我更改为白色,并更改它的值,例如,我将它改为#FFFFFF,以获得这样的白色值。

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

then inside your button add this line

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

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

ok b3 is the name of my button so changed of the name of ur button all others will be same if u use white color if you change different color then change white to the name of your color but first you have define that color in colors.xml like i explained in pont 2

好的,b3是我的按钮的名字所以改变了ur按钮的名字所有其他的都是一样的如果你使用白色如果你改变了不同的颜色然后改变白色为你的颜色的名字但是首先你用颜色定义了那个颜色。就像我在第2节中解释的那样

#4


1  

Changing text color of button

改变按钮的文本颜色

Because this method is now deprecated

因为现在不赞成使用这个方法

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

I use the following:

我使用下面的:

button.setTextColor(ContextCompat.getColor(mContext, R.color.your_color));

#1


514  

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

    <!-- 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>

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:

将xml放在res/drawable文件夹(即res/drawable/button_text_color.xml)中的文件中。然后将可绘制文本设置为:

android:textColor="@drawable/button_text_color"

#2


16  

Another way to do it is in your class:

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

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);

#3


2  

ok very simple first go to 1. res-valuse and open colors.xml 2.copy 1 of the defined text their for example #FF4081 and change name for instance i changed to white and change its value for instance i changed to #FFFFFF for white value like this

很简单,先到1。res-valuse和开放的颜色。xml 2。在定义的文本中复制1,例如#FF4081和更改名称,例如我更改为白色,并更改它的值,例如,我将它改为#FFFFFF,以获得这样的白色值。

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

then inside your button add this line

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

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

ok b3 is the name of my button so changed of the name of ur button all others will be same if u use white color if you change different color then change white to the name of your color but first you have define that color in colors.xml like i explained in pont 2

好的,b3是我的按钮的名字所以改变了ur按钮的名字所有其他的都是一样的如果你使用白色如果你改变了不同的颜色然后改变白色为你的颜色的名字但是首先你用颜色定义了那个颜色。就像我在第2节中解释的那样

#4


1  

Changing text color of button

改变按钮的文本颜色

Because this method is now deprecated

因为现在不赞成使用这个方法

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

I use the following:

我使用下面的:

button.setTextColor(ContextCompat.getColor(mContext, R.color.your_color));