[英]How do I change the text color of a Button?
如何更改按钮的文本颜色?
试试这个:
button.setTextColor(getApplication().getResources().getColor(R.color.red)); //TAKE DEFAULT COLOR
要么
button.setTextColor(0xff0000); //SET CUSTOM COLOR
要么
button.setTextColor(Color.parseColor("#ff0000"));
在 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 -->
使用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));
这工作太
在xml配置中使用: android:textColor="#FFFFFF"
,
或通过调用活动本身
button.setTextColor(0xFFFFFF);
(FFFFFF 是白色)。
更多颜色代码:这里
您可以将 android textColor 用于按钮、文本视图或任何其他元素的前景和背景颜色,请参见代码示例
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:background="#ffb6c1"
android:textColor="#fff"
/>
可以编写任何十六进制颜色代码来制作交互式视图。
以编程方式更改按钮文本颜色
button.setTextColor(getResources().getColor(R.color.colorWhite));
一种简单的方法是通过以下方式在 res/values/colors.xml 中定义您想要的颜色:
<color name="colorCyan">#00BCD4</color>
按钮应该是这样的:
<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"/>
这是一种代码略少的方法,它使用当前 Activity 的隐含上下文。
button.setTextColor(getColor(R.color.colorPrimary));
我没有对所有 API 目标进行测试,但它适用于 28 个。
您可以使用:
button.setTextColor("green");
要么
button.setTextColor(colorcode);
格式是 AARRGGBB,所以 FF0000 是透明的红色。 请改用 FFFF0000。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.