如何更改按钮的文本颜色?

时间:2022-04-05 09:39:22

How do I change the text color of a Button?

如何更改按钮的文本颜色?

7 个解决方案

#1


55  

try this:

button.setTextColor(getApplication().getResources().getColor(R.color.red)); //TAKE DEFAULT COLOR

or

button.setTextColor(0xff0000); //SET CUSTOM COLOR 

or

button.setTextColor(Color.parseColor("#ff0000")); 

and in xml :

并在xml中:

<Button android:id="@+id/mybtn" 
        android:text="text textx "  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"  
        android:textStyle="bold" 
        android:textColor="#ff0000" />  <-- SET TEXT COLOR HERE -->

#2


9  

Use the android:textColor property.

使用android:textColor属性。

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World"
    android:textColor="@android:color/white" />

#3


2  

Use: android:textColor="#FFFFFF" on the xml configuration,

在xml配置上使用:android:textColor =“#FFFFFF”,

or on the activity itself by calling

或通过致电活动本身

button.setTextColor(0xFFFFFF);

(FFFFFF is the color white).

(FFFFFF是白色)。

For more color codes: here

有关更多颜色代码:此处

#4


0  

You can use the android textColor for foreground and for background color of button, text view or any other element see code example

您可以使用android textColor作为前景,使用按钮,文本视图或任何其他元素的背景颜色查看代码示例

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:background="#ffb6c1"
        android:textColor="#fff"
        />

any hexadecimal color code can be written for making interactive view.

可以编写任何十六进制颜色代码以进行交互式视图。

#5


0  

An easy way to do this is by defining the color you want in res/values/colors.xml in this way:

一种简单的方法是通过这种方式在res / values / colors.xml中定义所需的颜色:

<color name="colorCyan">#00BCD4</color>

and the button should look this way:

按钮应该是这样的:

<Button
    android:id="@+id/m_button"
    android:text="MY BUTTON"
    android:textColor="@color/colorAccent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorCyan"/>

#6


0  

button.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.red));

this work too

这个工作也是

#7


0  

Here's an approach with slightly less code that uses the implied Context of the current Activity.

这是一种使用当前Activity的隐含Context的代码略少的方法。

button.setTextColor(getColor(R.color.colorPrimary));

I have not tested this with all API targets, but it is working for 28.

我没有对所有API目标进行测试,但它的工作时间为28。

#1


55  

try this:

button.setTextColor(getApplication().getResources().getColor(R.color.red)); //TAKE DEFAULT COLOR

or

button.setTextColor(0xff0000); //SET CUSTOM COLOR 

or

button.setTextColor(Color.parseColor("#ff0000")); 

and in xml :

并在xml中:

<Button android:id="@+id/mybtn" 
        android:text="text textx "  
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content"  
        android:textStyle="bold" 
        android:textColor="#ff0000" />  <-- SET TEXT COLOR HERE -->

#2


9  

Use the android:textColor property.

使用android:textColor属性。

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World"
    android:textColor="@android:color/white" />

#3


2  

Use: android:textColor="#FFFFFF" on the xml configuration,

在xml配置上使用:android:textColor =“#FFFFFF”,

or on the activity itself by calling

或通过致电活动本身

button.setTextColor(0xFFFFFF);

(FFFFFF is the color white).

(FFFFFF是白色)。

For more color codes: here

有关更多颜色代码:此处

#4


0  

You can use the android textColor for foreground and for background color of button, text view or any other element see code example

您可以使用android textColor作为前景,使用按钮,文本视图或任何其他元素的背景颜色查看代码示例

        <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        android:background="#ffb6c1"
        android:textColor="#fff"
        />

any hexadecimal color code can be written for making interactive view.

可以编写任何十六进制颜色代码以进行交互式视图。

#5


0  

An easy way to do this is by defining the color you want in res/values/colors.xml in this way:

一种简单的方法是通过这种方式在res / values / colors.xml中定义所需的颜色:

<color name="colorCyan">#00BCD4</color>

and the button should look this way:

按钮应该是这样的:

<Button
    android:id="@+id/m_button"
    android:text="MY BUTTON"
    android:textColor="@color/colorAccent"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorCyan"/>

#6


0  

button.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.red));

this work too

这个工作也是

#7


0  

Here's an approach with slightly less code that uses the implied Context of the current Activity.

这是一种使用当前Activity的隐含Context的代码略少的方法。

button.setTextColor(getColor(R.color.colorPrimary));

I have not tested this with all API targets, but it is working for 28.

我没有对所有API目标进行测试,但它的工作时间为28。