Android 如何更改按钮的文本颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11176365/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I change the text color of a Button?
提问by Jason Ching
How do I change the text color of a Button?
如何更改按钮的文本颜色?
回答by ρяσ?ρ?я K
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 -->
回答by James Cross
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" />
回答by Andreas
button.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.red));
this work too
这工作也是
回答by Nuno Gon?alves
回答by NomanJaved
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.
可以编写任何十六进制颜色代码来制作交互式视图。
回答by Abdul Basit Rishi
change button text color programmatically
以编程方式更改按钮文本颜色
button.setTextColor(getResources().getColor(R.color.colorWhite));
回答by Zahnon
Here's an approach with slightly less code that uses the implied Context of the current Activity.
这是一种使用当前活动的隐含上下文的代码略少的方法。
button.setTextColor(getColor(R.color.colorPrimary));
I have not tested this with all API targets, but it is working for 28.
我还没有对所有 API 目标进行测试,但它适用于 28。
回答by DragonS
You can use:
您可以使用:
button.setTextColor("green");
button.setTextColor("green");
or
或者
button.setTextColor(colorcode);
button.setTextColor(colorcode);
回答by Daniel Lobito Olvera
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>
<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"/>