简体   繁体   English

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

[英]How do I change the text color of a Button?

How do I change the text color of a Button?如何更改按钮的文本颜色?

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

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" />
button.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.red));

this work too这工作太

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更多颜色代码:这里

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.可以编写任何十六进制颜色代码来制作交互式视图。

change button text color programmatically以编程方式更改按钮文本颜色

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

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

Here's an approach with slightly less code that uses the implied Context of the current Activity.这是一种代码略少的方法,它使用当前 Activity 的隐含上下文。

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

I have not tested this with all API targets, but it is working for 28.我没有对所有 API 目标进行测试,但它适用于 28 个。

You can use:您可以使用:

button.setTextColor("green");

or要么

button.setTextColor(colorcode);

The format is AARRGGBB, so FF0000 is a transparent red.格式是 AARRGGBB,所以 FF0000 是透明的红色。 Use FFFF0000 instead.请改用 FFFF0000。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM