【问题标题】:How do I change the text color of a Button?如何更改按钮的文本颜色?
【发布时间】:2012-06-25 22:37:49
【问题描述】:

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

【问题讨论】:

    标签: android android-layout android-button


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 我知道这篇文章是两年前的,只是想强调一下 button.setTextColor(int color) 使用 Color 类的静态值,而不是定义你自己的十六进制颜色。例如。颜色.绿色
    • 我正在使用您的帖子android:textColor="#003EFF" 更改按钮的文本颜色,当我在.xml 页面上看到它时它工作正常但是当我运行模拟器并检查它时,它没有改变按钮:文本颜色。为什么?
    • button.setTextColor(0xFF0000); 不适用于 API 19
    【解决方案2】:

    使用android:textColor 属性。

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

    【讨论】:

      【解决方案3】:
      button.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.red));
      

      这也是工作

      【讨论】:

        【解决方案4】:

        在xml配置上使用:android:textColor="#FFFFFF"

        或调用 Activity 本身

        button.setTextColor(0xFFFFFF);
        

        (FFFFFF 是白色)。

        更多颜色代码:here

        【讨论】:

        • 与我添加到 ρяσѕρєя K 的答案中的评论相同,button.setTextColor(int color) 使用来自 Color 类的静态值。他的下一行是正确的: button.setTextColor(Color.parseColor("#ff0000"));
        【解决方案5】:

        您可以将 android textColor 用于前景和按钮、文本视图或任何其他元素的背景颜色,请参见代码示例

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

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

        【讨论】:

          【解决方案6】:

          以编程方式更改按钮文本颜色

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

          【讨论】:

            【解决方案7】:

            一个简单的方法是在 res/values/colors.xml 中定义你想要的颜色:

            &lt;color name="colorCyan"&gt;#00BCD4&lt;/color&gt;

            按钮应该是这样的:

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

            【讨论】:

              【解决方案8】:

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

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

              我还没有对所有 API 目标进行测试,但它适用于 28。

              【讨论】:

                【解决方案9】:

                你可以使用:

                button.setTextColor("green");

                button.setTextColor(colorcode);

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2021-11-12
                  • 2013-06-05
                  • 2019-02-14
                  • 1970-01-01
                  • 2020-11-07
                  • 1970-01-01
                  • 2019-12-17
                  相关资源
                  最近更新 更多