2016-07-07 164 views
0

我有一個自定義樣式的複選框,我想在我的Android應用中複製多次。此複選框的XML是:Android自定義XML視圖模板

<CheckBox 
    android:layout_marginLeft="10dp" 
    android:layout_marginTop="10dp" 
    android:paddingLeft="10dp" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:button="@drawable/checkbox_large" 
    android:text="Temporary Text"/> 

很顯然,我可以簡單地複製XML並粘貼降低倒在我的佈局文件,但我想改變設計的情況下我不得不這樣做對每個粘貼的複選框。

是否有一個乾淨的方式來創建這個複選框的自定義模板,或者我應該在代碼中實現它?

回答

3

是否有一個乾淨的方式來創建這個複選框的自定義模板,或者我應該在代碼中實現它?

您可以使用styles。創建

<style name="MyCheckbox"> 
    <item name="android:layout_marginLeft">10dp</item> 
    <item name="android:layout_marginTop">10dp</item> 
    <item name="android:paddingLeft">10dp</item> 
    <item name="android:layout_width">wrap_content</item> 
    <item name="android:layout_height">wrap_content</item> 
    <item name="android:button">@drawable/checkbox_large</item> 
    <item name="android:text">Temporary Text</item> 
</style> 

,然後只適用於你希望所有的複選框:

<CheckBox 
    style="@style/MyCheckbox"/> 

當需要改變的,只是改變你的風格。