2010-12-16 19 views
6

我已經定義了一些包含TextAppearance和定義的TextColor的樣式資源。然後我將這些樣式應用到一些TextViews和Buttons。所有樣式都是通過TextView來實現的,但不是按鈕。出於某種原因,textColor屬性不顯示。這是一個錯誤,還是我錯過了按鈕的情況?在應用於TextView的樣式中定義的TextColor,但不是Button?

這裏是樣式定義:

<?xml version="1.0" encoding="UTF-8"?> 
<resources> 

    <style name="TestApp">  
    </style> 

    <!-- Text Appearances --> 
    <style name="TestApp.TextAppearance"> 
     <item name="android:typeface">sans</item> 
     <item name="android:textStyle">bold</item> 
     <item name="android:textSize">16px</item>  
     <item name="android:textColor">#6666FF</item> 
    </style> 

    <!-- Widget Styles --> 
    <style name="TestApp.Widget"> 
     <item name="android:layout_margin">3sp</item> 
    </style> 

    <style name="TestApp.Widget.Label"> 
     <item name="android:textAppearance">@style/TestApp.TextAppearance</item> 
     <item name="android:layout_width">wrap_content</item> 
     <item name="android:layout_height">wrap_content</item> 
    </style> 

    <style name="TestApp.Widget.Switch"> 
     <item name="android:textAppearance">@style/TestApp.TextAppearance</item> 
     <item name="android:layout_width">100px</item> 
     <item name="android:layout_height">100px</item> 
    </style> 

</resources> 

這裏的地方我嘗試應用它們的佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 
<TextView 
    style="@style/TestApp.Widget.Label" 
    android:text="This is my label." /> 
<TextView 
    style="@style/TestApp.Widget.Label" 
    android:text="This is my disabled label." 
    android:enabled="false" /> 
<Button 
    style="@style/TestApp.Widget.Switch" 
    android:text="This is my switch." /> 
<Button 
    style="@style/TestApp.Widget.Switch" 
    android:text="This is my disabled switch." 
    android:enabled="false" /> 
</LinearLayout> 

回答

1

對於按鈕的情況,有兩種方法可以通過屬性定義文本顏色:textColor並通過textAppearance定義的樣式。

textColor(由默認樣式設置)設置的值將覆蓋由textAppearance樣式設置的任何文本顏色值。因此,您必須以兩種方式之一將textColor屬性設置爲@null

  • 設置文字顏色的風格@null:

    <style name="Application.Button"> 
        <item name="android:textAppearance">@style/Application.Text.Medium.White</item> 
        <item name="android:textColor">@null</item> 
    </style> 
    
    <Button 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        style="@style/Application.Button" /> 
    
  • 設置文字顏色的按鈕XML到@null:

    <Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    style="@style/Application.Button" 
    android:textColor="@null" /> 
    
相關問題