2012-06-27 29 views
3

我在我的應用程序中爲字體創建了幾種樣式。android:set textColor with textAppearance屬性

如何將這些樣式添加到視圖? - 1)使用樣式屬性2)使用textAppearance。

使用樣式不是一種選擇,因爲視圖可能具有其他屬性(邊距,填充,最小寬度等 - 我無法爲視圖指定2種樣式),所以我需要單獨指定與文本相關的屬性,但android:textColor不工作在這裏:

styles.xml:

<style name="TextAppearance.baseText" parent="@android:style/TextAppearance"> 
    <item name="android:textAppearance">?android:textAppearanceSmall</item> 
    <item name="android:typeface">sans</item> 
    <item name="android:textColor">@android:color/white</item> 
    <item name="android:textSize">15sp</item> 
</style> 

layout.xml:

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     android:layout_width="match_parent" 
     android:layout_height="65dp" 
     android:orientation="horizontal" > 

     <Button 
      android:id="@+id/backButton" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/sometext" 
      android:textAppearance="@style/TextAppearance.baseText"/> 
</RelativeLayout> 

爲什麼犯規工作?我怎樣才能在textappearance中指定textcolor?

回答

7

它的工作原理。您的文字顏色是白色 - 與默認顏色相同。把那裏的其他顏色,如<item name="android:textColor">#AA6699</item>,你會看到不同。 第二件事是你試圖在文本外觀中設置文本外觀 - doen是有道理的。如果要設置小寫字母,請使用父母parent="@android:style/TextAppearance.Small和刪除線<item name="android:textAppearance">?android:textAppearanceSmall</item> 執行此操作。第三件事是,您希望使用小寫字母並添加其他文本大小 - 沒有意義。 試試這個,對我的作品:

<style name="BaseText" parent="@android:style/TextAppearance.Small"> 
     <item name="android:typeface">sans</item> 
     <item name="android:textColor">#AA7755</item> 
    </style> 

如果你想設置的款式應有盡有:

<style name="BaseText" parent="@android:style/TextAppearance.Large"> 
     <item name="android:typeface">sans</item> 
     <item name="android:textColor">#AA7755</item> 
    </style> 

    <style name="BaseText.Margins"> 
     <item name="android:layout_margin">10dip</item> 
    </style> 

凡BaseText.Margins從BaseText繼承。 然後,你可以使用:

<TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="TextView" 
     style="@style/BaseText.Margins" /> 
+0

機器人正確繼承。我嘗試了Api8和Api15上的BaseText。關於BaseText.Margins方法 - 我知道它,但它意味着創建幾個樣式,如果幾個不同外觀的視圖具有相同的textStyle。這可能會導致大量的風格 - 我寧願避免這種情況。 –

+0

好吧,所以你的問題不是風格,你的問題是你想將它設置爲按鈕。最簡單的方法是將TextView改爲Button,並使用選擇器使其可點擊。那麼你可以使用你的風格:) – ania

+0

Yeap!這真的起作用了,謝謝!說實話,我認爲這一切都可能是由於我使用按鈕而不是textview的事實......但我不相信這可能是真的,並沒有測試它,因爲按鈕擴展了textview) –

8

由於Oderik在評論中提到,該按鈕來查看有一個默認值是textColor屬性。此textColor值將覆蓋通過textAppearance屬性設置的任何值。因此,你有兩個選擇:

  • 設置文字顏色的風格@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" /> 
    
2

如果要使用android:TextAppearance而不是style:來設置android:textColor,則需要se t android:[email protected]在您的TextView。對我來說文字顏色不工作:

<TextView xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:textAppearance="@style/TextAppearance.AppCompat.Medium.MySubheader" 
    android:textColor="@null" 
    tools:text="Section" /> 

然後,它會從你的android:TextAppearance

<style name="TextAppearance.AppCompat.Medium.MySubheader"> 
    <item name="android:fontFamily">sans-serif-medium</item> 
    <item name="android:textSize">16sp</item> 
    <item name="android:textColor">@color/black_54</item> 
</style> 
相關問題