gpt4 book ai didi

android - CheckBox 如何获取其各自的可绘制对象?

转载 作者:行者123 更新时间:2023-12-02 16:14:51 24 4
gpt4 key购买 nike

CheckBox 类扩展了CompoundButton,但没有添加任何内容。但它是如何获得各自的外观的。我在 Android 源代码中找到了一些声明,但想知道它们是如何映射到 CheckBox 类的?

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>

编辑:

可能我不清楚...我明白如何将drawable分配给Widget.CompoundButton.CheckBox样式,但是如何将这种样式分配给CheckBox类?我在样式名称中看到“.CheckBox”,但这种命名约定真的是这个技巧的关键吗?如果是的话,规则是什么?如果我从CompoundButton派生MyCheckBox,我可以只定义Widget.CompoundButton.MyCheckBox样式并且它会起作用吗?

最佳答案

这就是你的答案:<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);
}

构造函数将该属性分配给 View 。就是这样。

关于android - CheckBox 如何获取其各自的可绘制对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4663314/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com