2011-01-11 24 views
2

CheckBox類擴展了CompoundButton,但沒有添加任何內容。但有些如何獲得它的相應外觀。我在Android源代碼中發現了一些聲明,但不知道它們如何映射到CheckBox類?CheckBox如何獲取相應的drawables?

public class CheckBox extends CompoundButton { 
    public CheckBox(Context context) { 
     this(context, null); 
    } 

    public CheckBox(Context context, AttributeSet attrs) { 
     this(context, attrs, com.android.internal.R.attr.checkboxStyle); 
    } 

    public CheckBox(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 
} 

樣式

<style name="Theme"> 
    <item name="checkboxStyle">@android:style/Widget.CompoundButton.CheckBox</item> 
</style> 

<style name="Widget.CompoundButton.CheckBox"> 
    <item name="android:background">@android:drawable/btn_check_label_background</item> 
    <item name="android:button">@android:drawable/btn_check</item> 
</style> 

編輯:

也許我並不清楚......我明白瞭如何繪製分配給Widget.CompoundButton.CheckBox風格,但這種風格如何分配到CheckBox類?我在樣式名稱中看到了「.CheckBox」,但是這個命名約定究竟是什麼原因?如果是這樣,規則是什麼?如果我從CompoundButton派生MyCheckBox,我可以只定義Widget.CompoundButton.MyCheckBox樣式,它會工作嗎?

回答

4

有你的答案:<item name="android:button">@android:drawable/btn_check</item>

被拉伸可以有多個州(元素是否被關注,壓制,等等),而這也正是的不同狀態複選框是。

編輯:當你修改你的問題後,我看到你正在尋找不同的東西。再次,它全部在你發佈的內容中:

<style name="Widget.CompoundButton.CheckBox"> 

這是風格。

<item name="checkboxStyle">@android:style/Widget.CompoundButton.CheckBox</item> 

主題(android.internal.R.attr)具有指向此樣式的屬性「checkboxStyle」。

public CheckBox(Context context, AttributeSet attrs) { 
    this(context, attrs, com.android.internal.R.attr.checkboxStyle); 
} 

構造函數將該屬性分配給視圖。就是這樣。

+0

哦..的確,我忽略了checkboxStyle。 TY! – alex2k8 2011-01-11 22:57:16

1

就在這裏

<item name="android:button">@android:drawable/btn_check</item> 
+0

我想我不清楚,所以編輯了這個問題。 – alex2k8 2011-01-11 22:42:18

相關問題