2010-12-16 21 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" /> 
    
相关问题