2011-11-23 59 views
6

我可以通過這樣的內部對象正確設置它改變按鈕文本外觀:的Android按鈕textAppearance

<Button 
     android:id="@+id/login_btn_bypass" 
     android:textSize="15dp" 
     android:textColor="#878787" 
     android:textStyle="bold" /> 

而不是內使用風格時textAppearance

// in layout xml 
<Button 
    android:id="@+id/login_btn_login" 
    android:textAppearance="@style/login_button_text_appearance" /> 

// in style definition 
<style name="login_button_text_appearance"> 
    <item name="android:textSize">15dp</item> 
    <item name="android:textColor">#a7a7a7</item> 
    <item name="android:textStyle">bold</item> 
</style> 

任何人知道這是爲什麼?

回答

9

我認爲你應該使用:

style = "@style/login_button_text_appearance"

,而不是

android:textAppearance="@style/login_button_text_appearance"

android:textAppearance就像任何其他attribut(android:textSize,android:textStyle ...等)的attribut,和該款式的價值是不可接受的作爲該屬性的值

編輯:

<Button 
    android:id="@+id/login_btn_login" 
    style="@style/login_button_text_appearance" /> 
+0

我在Button上看不到任何android:style,應該如何應用您的建議? –

+0

沒有attribut命名的android:style,它直接命名style,看我的編輯 – Houcine

11

使用textAppearance定義的屬性的值的風格屬性的值之前被應用。 A ButtonTextView,應用了一種樣式,Button的默認樣式將覆蓋textAppearance(例如,Android 2.3將它設置爲?android:attr/textAppearanceSmallInverse)和textColor。

textAppearance節選的風格作爲值,android:textAppearance="@style/login_button_text_appearance"是設置textAppearance常正確的方法,而不是一個Button

如果你改變一個Button的文本顏色,你也應該執行自定義背景圖像,因爲如果你不這樣做,一臺設備將使用黑色背景圖像(摩托羅拉defy),另一臺將使用光圖像(htc慾望),這可能會使文本難以閱讀。