[Android UI界面] 解讀CheckBox

打開源碼中CheckBox.java文件,我們可以看到如下內容:
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);
    }
}
可以看到,CheckBox是繼承於CompoundButton,而CompoundButton:
public abstract class CompoundButton extends Button implements Checkable
是Button的一個子類,由此可知,CheckBox也爲Button的一個子類。從上面的代碼中可以看出,CheckBox的不同之處就是添加了屬性checkboxStyle,我們在源碼中找到該樣式的定義如下:
<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>
其中,在資源文件中找到btn_check_label_background,該圖片是背景透明沒有內容的一張圖片,因此CheckBox的顯示就是由btn_check決定,我們打開btn_check.xml文件,可以看到以下內容:
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <!-- Enabled states -->
        
    <item android:state_checked="true" android:state_window_focused="false"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_on" />
    <item android:state_checked="false" android:state_window_focused="false"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_off" />

    <item android:state_checked="true" android:state_pressed="true"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_on_pressed" />
    <item android:state_checked="false" android:state_pressed="true"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_off_pressed" />

    <item android:state_checked="true" android:state_focused="true"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_on_selected" />
    <item android:state_checked="false" android:state_focused="true"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_off_selected" />

    <item android:state_checked="false"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_off" />
    <item android:state_checked="true"
          android:state_enabled="true"
          android:drawable="@drawable/btn_check_on" />


    <!-- Disabled states -->

    <item android:state_checked="true" android:state_window_focused="false"
          android:drawable="@drawable/btn_check_on_disable" />
    <item android:state_checked="false" android:state_window_focused="false"
          android:drawable="@drawable/btn_check_off_disable" />

    <item android:state_checked="true" android:state_focused="true"
          android:drawable="@drawable/btn_check_on_disable_focused" />
    <item android:state_checked="false" android:state_focused="true"
          android:drawable="@drawable/btn_check_off_disable_focused" />

    <item android:state_checked="false" android:drawable="@drawable/btn_check_off_disable" />
    <item android:state_checked="true" android:drawable="@drawable/btn_check_on_disable" />

</selector>
每一個item就對應着CheckBox在不同的選擇狀態下顯示的效果,瞭解該文件的內容後,我們就可以更具自己的需要來設計美觀的CheckBox。RadioButton的顯示與CheckBox基本相似,可以仿照CheckBox加以修改。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章