2015-09-05 84 views
1

我創造了這個可繪製在我的按鈕在我的應用程序中使用:Android的 - 將按鈕樣式爲主題

btnprimary.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/btnprimary_disabled" android:state_enabled="false" /> 
    <item android:drawable="@drawable/btnprimary_pressed" android:state_enabled="true" android:state_pressed="true" /> 
    <item android:drawable="@drawable/btnprimary_focused" android:state_enabled="true" android:state_focused="true" /> 
    <item android:drawable="@drawable/btnprimary_enabled" android:state_enabled="true" /> 
</selector> 

每個州都有一個drawable太多,所以,例如,這是btnprimary_enabled

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > 
    <corners 
     android:radius="5dp" 
     /> 
    <gradient 
     android:angle="90" 
     android:startColor="#335BB8" 
     android:endColor="#6397FF" 
     android:type="linear" 
     /> 
    <padding 
     android:left="0dp" 
     android:top="0dp" 
     android:right="0dp" 
     android:bottom="0dp" 
     /> 
    <stroke 
     android:width="1dp" 
     android:color="#878787" 
     /> 
</shape> 

所以文件,我可以在這樣的按鈕使用,使用背景p roperty:

<Button 
     android:id="@+id/btn_login" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:background="@drawable/btnprimary" 
     android:text="@string/login" 
     android:textColor="#FFFFFF" 
     android:gravity="center"/> 

而且我有我的主題styles.xml,但現在是空的。就像這樣:

<resources> 

    <!-- Base application theme. --> 
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> 
     <!-- Customize your theme here. --> 
    </style> 

</resources> 

我的問題是,我可以把我的drawable創建的那些配置成我的主題,所有的按鈕具有這種風格,而不需要添加background財產?

回答

2

你有沒有試過這段代碼?在你的風格文件中,定義按鈕風格

<style name="ApplicationStyle" parent="android:Theme"> 
<item name="android:buttonStyle">@style/yourButton</item> 
</style> 
+0

是的,that1s我在說什麼!謝謝 –