簡體   English   中英

Android:在樣式中設置按鈕文字的顏色

[英]Android: Set button text color in style

我想創建一個我所有按鈕都使用的按鈕樣式,這樣我就不需要在每個按鈕上設置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"

該按鈕獲取背景顏色,但文本顏色保持黑色。

我究竟做錯了什么?

您需要將顏色屬性添加到項目標簽以設置文本的顏色。

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

您可以使用樣式進行設置:

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

您可以轉到“ styles.xml”文件,可以在“ values”文件夾中找到它,並將以下行添加到該文件中:

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

之后,當您添加按鈕時,將此行添加到按鈕屬性中。

style="@style/buttonstyle"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM