【问题标题】:Android: Set button text color in styleAndroid:在样式中设置按钮文本颜色
【发布时间】:2015-05-11 00:08:30
【问题描述】:

我想创建一个所有按钮都使用的按钮样式,这样我就不需要在每个按钮上设置 android:textColor。

我在一个名为 defaultbutton.xml 的文件中有这个样式

<item android:state_pressed="false" android:state_focused="false">
    <shape android:shape="rectangle">
        <solid android:color="#F3C22C"/>
        <corners android:radius="10dp" />
    </shape>
    <color android:color="@color/brown"></color>
</item>

对于我设置的测试按钮:

android:background="@drawable/defaultbutton"

按钮获得背景颜色,但文本颜色保持为黑色。

我做错了什么?

【问题讨论】:

标签: android


【解决方案1】:

你需要给item标签添加color属性来设置文本的颜色。

/res/drawable/custom_color.xml

<item android:state_pressed="false" android:state_focused="false"
      android:color="@color/text_color"> //this is how text color can be defined

</item>

现在将 textColor 设置为布局的 textView

<TextView  ...
    android:textColor="@drawable/custom_color">

【讨论】:

  • 所以我还是需要在元素级别设置textColor属性,这是我不想做的
  • 是的@andrewb,你必须这样做。而且,如果您想设置按钮背景的样式,那么您发布的 xml 就可以设置背景样式。您还需要添加它,例如,android:background="your_style"
【解决方案2】:

你可以设置样式:

<style name="MyButtonText" parent="@android:style/Widget.Button">
<item name="android:textColor">@color/mycolor</item>

【讨论】:

    【解决方案3】:

    您可以转到“styles.xml”文件,您可以在“values”文件夹中找到它 并将以下几行添加到文件中:

    <style name="buttonstyle" >
            <item name="android:textColor" >#ff11</item>
    </style>
    

    之后,当您添加按钮时,将此行添加到您的按钮属性中。

    style="@style/buttonstyle"
    

    【讨论】:

      猜你喜欢
      • 2011-11-23
      • 1970-01-01
      • 2015-12-07
      • 2012-12-11
      • 2011-07-16
      • 1970-01-01
      • 2012-09-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多