2012-04-24 27 views
3

我能夠宣佈主題和特定的按鈕設計:的Android應用主題的自定義項目

<style name="MyTheme" parent="android:style/Theme.Black"> 
    <item name="android:buttonStyle">@style/Button.b1</item> 
</style> 

<style name="Button.b1" parent="android:style/Widget.Button"> 
    <item name="android:textColor">#000000</item> 
</style> 

的問題是,這種風格應用到所有按鈕。我想用自己的風格來聲明一個特定的按鈕,以獨立於其他按鈕來改變每個主題(就像「保存」按鈕一樣)。任何想法?


我試過如下:

<style name="Button.MyButton" parent="android:style/Widget.Button"> 
    <item name="android:background">@drawable/shape</item> 
</style> 

<style name ="Button.MyButton.Theme1"> 
    <item name="android:textColor">#000000</item> 
    </style> 

<style name ="Button.MyButton.Theme2"> 
    <item name="android:textColor">#FFFFFF</item> 
    </style> 

<Button 
    android:id="@+id/save_button" 
    android:layout_width="0px" 
    style="@style/Button.MyButton" 
    android:layout_weight="1" 
    android:layout_height="wrap_content" 
    android:text="@string/save"/> 

現在按鈕是獨立於主題,但它應該也適用,我上面定義的樣式屬性。目前它只應用MyButton範圍中聲明的屬性,而不適用於MyButton.Theme1或MyButton.Theme2中的屬性。

+0

爲每個按鈕創建單獨的主題... – 2012-04-24 09:44:51

+0

我正在做類似的事情! http://stackoverflow.com/questions/17103894/overriding-referenced-style-attributes – toobsco42 2013-06-14 08:12:08

回答

1

您可以從styles.xml刪除MyTheme聲明,並很容易地在你的選擇:)

編輯按鈕的android:style屬性設置@style/Button.b1:我想,我終於得到了你問什麼。我沒有嘗試過自己,但它應該工作:

<!-- Declare your themes here --> 
<style name="Theme1" parent="android:style/Theme.Black"></style> 
<style name="Theme2" parent="android:style/Theme.Black"></style> 

<!-- Declare your "abstract" Button style --> 
<style name="MyButton" parent="android:style/Widget.Button"> 
    <item name="android:background">@drawable/shape</item> 
</style> 

<!-- Override MyButton style for Theme1 --> 
<style name ="Theme1.MyButton" parent="@style/MyButton"> 
    <item name="android:textColor">#000000</item> 
</style> 

<!-- Override MyButton style for Theme2 --> 
<style name ="Theme2.MyButton" parent="@style/MyButton"> 
    <item name="android:textColor">#FFFFFF</item> 
</style> 
+0

這不完全是我想要的 - 我更新了我的問題。 – Anthea 2012-04-24 10:14:08

0

你應該用你Button.b1風格直接只爲你希望被稱呼這樣的按鈕。使用這些按鈕android:style屬性:

android:style="Button.b1" 
3

如果你的主題MyTheme只是用來定義按鈕樣式,將其刪除;在佈局

<style name="Button.b1"> 
    <item name="android:textColor">#000000</item> 
</style> 

然後,使用風格只爲那些需要像下面的按鈕:還從按鈕移除parent財產

<Button 
     android:id="@+id/btn_custom" 
     style="@style/Button.b1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 
</Button> 
+0

Mytheme在這裏比較大,但它應該包含一個特定的樣式元素,其中包含僅用於保存按鈕的信息。如果我改變主題,保存按鈕也應該以他自己的方式改變。 – Anthea 2012-04-24 10:11:23

+0

這並不意味着您必須在MyTheme中設置「保存」按鈕樣式。你如何改變主題?編程? – Adinia 2012-04-24 10:17:07

+0

因爲它應該在運行時,我想我會在每個活動的開始以編程方式執行此操作。 – Anthea 2012-04-24 10:18:54