2012-12-24 51 views
5

我想申請默認情況下,按鈕上的文字顏色styles.xmlAndroid的風格指定按鈕顏色

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 
<style name="AppTheme"> 
    <item name="android:windowFullscreen">true</item> 
    <item name="android:windowBackground">@color/black</item> 
    <item name="android:textColor">@color/green</item> 
</style> 

如何讓我的風格轉換按鈕的顏色,並應用在整個應用程序?我的主要包括:

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="Hack the World" 
    android:theme="@style/AppTheme"> 

這改變了所有文本(如TextView),但不改變按鈕中的文本。如果我使用從上面的ColorThemes風格,並將其放置在XML像這樣的按鈕:

<Button 
    android:id="@+id/button3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_centerHorizontal="true" 
    android:layout_centerVertical="true" 
    android:onClick="loadSavedgame" 
    android:text="Load Saved Game" 
    android:textColor="@color/green" />" 

然後,它完美的作品。爲什麼這不適用於普遍的風格?所有不同版本的styles.xml具有相同的代碼。

回答

14

我最終發現一個按鈕實際上是位於@android:drawable/btn_default的9補丁圖像,爲了更改背景,您必須修改此圖像。爲了改變按鈕的文字顏色,我不得不設置一個按鈕小部件樣式,並將其導入到我的應用程序主題中,如下所示:

<resources xmlns:android="http://schemas.android.com/apk/res/android"> 
<style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar" > 
    <item name="android:windowFullscreen">true</item> 
    <item name="android:windowBackground">@color/black</item> 
    <item name="android:textColor">@color/green</item> 
    <item name="android:buttonStyle">@style/ButtonText</item> 
</style> 

    <style name="ButtonText" parent="@android:style/Widget.Button"> 
    <item name="android:textColor">@color/green</item> 
    <item name="android:background">@android:drawable/btn_default</item><!-- only way to change this is to change the src image --> 
    <item name="android:layout_width">80.0px</item> 
    <item name="android:layout_height">40.0px</item> 
</style> 
</resources>