开发者

How does the CheckBox obtain it's respective drawables?

开发者 https://www.devze.com 2023-02-03 13:25 出处:网络
The CheckBox class extends the CompoundButton, but add nothing to it. But some how it o开发者_开发知识库btains it\'s respective look. I found some declarations in Android sources, but wonder how they

The CheckBox class extends the CompoundButton, but add nothing to it. But some how it o开发者_开发知识库btains it's respective look. I found some declarations in Android sources, but wonder how they are mapped to CheckBox class?

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);
    }
}

Styles

<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>

EDIT:

Probably I was not clear... I understand how the drawable assigned to Widget.CompoundButton.CheckBox style, but how this style assigned to CheckBox class? I see the ".CheckBox" in the style name, but is this naming convention really what makes the trick? If so, what are the rules? If I derive MyCheckBox from CompoundButton, can I just define the Widget.CompoundButton.MyCheckBox style and it will work?


There's your answer: <item name="android:button">@android:drawable/btn_check</item>

A drawable can have multiple states (whether or not the element is focused, pressed, etc), and that's where the different states of the checkbox are.

EDIT: After you revised your question, I see that you're looking for something different. Again, it's all in what you posted:

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

This is the style.

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

The main theme (android.internal.R.attr) has an attribute "checkboxStyle" that points to this style.

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

The constructor assigns that attribute to the view. And that's it.


Right here

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

精彩评论

暂无评论...
验证码 换一张
取 消