2015-11-01 23 views
2

我想要定義樣式上的按鈕屬性。 我的代碼是無法繼承樣式上的按鈕顏色

<style name="button"> 
    <item name="android:paddingTop">5dp</item> 
    <item name="android:paddingBottom">5dp</item> 
    <item name="android:textAllCaps">false</item> 
    <item name="colorButtonNormal">#f37022</item> 
    <item name="android:textColor">#ffffff</item> 
</style> 

而且使用作爲

<Button 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="Prepaid Package" 
     style="@style/button"/> 

但按鈕的正常狀態的顏色沒有改變。

此按鈕樣式僅適用於某些按鈕。不是所有的butons。

現在該做什麼?問題澄清後

回答

1

編輯

保存您在res /值style.xml/

<?xml version="1.0" encoding="utf-8"?> 
<resources> 
    <style name="custom_button" parent="@android:style/Widget.Button"> 
     <item name="android:paddingTop">5dp</item> 
     <item name="android:paddingBottom">5dp</item> 
     <item name="android:textAllCaps">false</item> 
     <item name="colorButtonNormal">#f37022</item> 
     <item name="android:textColor">#ffffff</item> 
    </style> 

應用它像這樣:

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Prepaid Package" 
    style="@style/custom_button"/> 

第一個答案

您需要繼承父級樣式。

<style name="Button" parent="@android:style/Widget.Button"> 
    <item name="android:paddingTop">5dp</item> 
    <item name="android:paddingBottom">5dp</item> 
    <item name="android:textAllCaps">false</item> 
    <item name="colorButtonNormal">#f37022</item> 
    <item name="android:textColor">#ffffff</item> 
</style> 

這樣,所有未設置樣式的按鈕都將具有您的自定義樣式。即不需要輸入樣式值。

<Button 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:text="Prepaid Package"/> 
+0

這是否意味着'colorButtonNormal'將應用於所有按鈕? – Sudarshan

+0

@Sudarshan你創建的新風格將適用於所有的按鈕,除非你給他們一個新的風格。如果這不起作用,那麼這意味着你已經錯誤地設置了你的樣式資源。 –

+0

但我不想將這種風格應用於所有按鈕。 我想爲某些按鈕申請此樣式。 – Sudarshan