Android:在样式中设置按钮文本颜色

时间:2015-03-09 17:58:42

标签: android

我想创建一个按钮样式,我的所有按钮都使用,这样我就不需要在每个按钮上设置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"

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

我做错了什么?

3 个答案:

答案 0 :(得分:1)

您需要将颜色属性添加到item标记以设置文本颜色。

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

答案 1 :(得分:0)

您可以使用样式设置它:

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

答案 2 :(得分:0)

你可以去&#34; styles.xml&#34;文件,您可以在&#34;值&#34;中找到它。夹 并将以下行添加到文件中:

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

之后添加按钮时,将此行添加到按钮attr。

style="@style/buttonstyle"
相关问题