我是 android 新手,我正在尝试为我的应用程序中的所有复选框设置样式。我的应用程序样式设置为 Theme.Holo,它是深色的,我希望 ListView 上的复选框的样式为 Theme.Holo.Light。我不是在尝试创建自定义样式。下面的代码似乎不起作用,根本没有任何 react 。我需要这样做,因为我的 ListView 具有浅色纸张纹理,并且复选框和复选框文本是白色的,我想要深色。
<style name="CustomActivityTheme" parent="@android:style/Theme.Holo">
<item name="android:checkboxStyle">@style/customCheckBoxStyle</item>
</style>
<style name="customCheckBoxStyle" parent="@android:style/Widget.Holo.Light.CompoundButton.CheckBox">
</style>
如果您为应用程序设置样式,您还可以为单个小部件设置样式吗?
最佳答案
备注 :使用 Android 支持库 v22.1.0 并针对 API 级别 11 及更高版本?向下滚动到最后一次更新。
My application style is set to Theme.Holo which is dark and I would like the check boxes on my list view to be of style Theme.Holo.Light. I am not trying to create a custom style. The code below doesn't seem to work, nothing happens at all.
起初,系统为什么会表现出这种行为可能并不明显,但当你真正研究机制时,你可以很容易地推断出它。让我一步一步地带你过去。
首先我们来看看
Widget.Holo.Light.CompoundButton.CheckBox
是什么风格定义。为了让事情更清楚,我还添加了“常规”(非灯光)样式定义。<style name="Widget.Holo.Light.CompoundButton.CheckBox" parent="Widget.CompoundButton.CheckBox" />
<style name="Widget.Holo.CompoundButton.CheckBox" parent="Widget.CompoundButton.CheckBox" />
如您所见,这两个都是空声明,只是简单地包装了
Widget.CompoundButton.CheckBox
以不同的名称。因此,让我们看一下父样式。<style name="Widget.CompoundButton.CheckBox">
<item name="android:background">@android:drawable/btn_check_label_background</item>
<item name="android:button">?android:attr/listChoiceIndicatorMultiple</item>
</style>
此样式同时引用背景和按钮可绘制对象。
btn_check_label_background
只是一个 9 补丁,因此在这个问题上不是很有趣。但是,?android:attr/listChoiceIndicatorMultiple
表示基于 的某些属性当前主题 (认识到这一点很重要)将决定 CheckBox
的实际外观。 .如
listChoiceIndicatorMultiple
是一个主题属性,你会发现它的多个声明 - 每个主题一个(或者如果它是从父主题继承的,则没有)。这将如下所示(为清楚起见省略了其他属性):<style name="Theme">
<item name="listChoiceIndicatorMultiple">@android:drawable/btn_check</item>
...
</style>
<style name="Theme.Holo">
<item name="listChoiceIndicatorMultiple">@android:drawable/btn_check_holo_dark</item>
...
</style>
<style name="Theme.Holo.Light" parent="Theme.Light">
<item name="listChoiceIndicatorMultiple">@android:drawable/btn_check_holo_light</item>
...
</style>
所以这就是真正的魔法发生的地方:基于主题的
listChoiceIndicatorMultiple
属性,CheckBox
的实际外观决心,决意,决定。您看到的现象现在很容易解释:因为外观是基于主题的( 不是基于 样式的,因为这只是一个空定义),并且您继承自 Theme.Holo
,您将始终得到 CheckBox
外观与主题相匹配。现在,如果您想更改您的
CheckBox
的外观到 Holo.Light 版本,您需要获取这些资源的副本,将它们添加到本地 Assets 并使用自定义样式来应用它们。至于你的第二个问题:
Also can you set styles to individual widgets if you set a style to the application?
当然,它们将覆盖任何 Activity 或应用程序集样式。
Is there any way to set a theme(style with images) to the checkbox widget. (...) Is there anyway to use this selector: link?
更新:
让我首先再说一遍,你不应该依赖 Android 的内部资源。您不能随意访问内部命名空间是有原因的。
然而,访问系统资源的方法毕竟是通过名称进行 id 查找。考虑以下代码 fragment :
int id = Resources.getSystem().getIdentifier("btn_check_holo_light", "drawable", "android");
((CheckBox) findViewById(R.id.checkbox)).setButtonDrawable(id);
第一行实际上将返回
btn_check_holo_light
的资源 ID可绘制资源。由于我们之前确定这是决定 CheckBox
外观的按钮选择器。 ,我们可以在小部件上将其设置为“可绘制按钮”。结果是 CheckBox
与Holo.Light
的外观版本,无论您在 xml 中的应用程序、 Activity 或小部件上设置什么主题/样式。由于这只设置了可绘制的按钮,因此您需要手动更改其他样式;例如关于文本外观。下面是显示结果的屏幕截图。顶部的复选框使用上述方法(我在xml中手动将文本颜色设置为黑色),而第二个使用默认的基于主题的
Holo
造型(非轻,即)。更新 2:
与 the introduction of Support Library v22.1.0 ,事情变得容易多了!发行说明中的引用(我的重点):
Lollipop added the ability to overwrite the theme at a view by view level by using the
android:theme
XML attribute - incredibly useful for things such as dark action bars on light activities. Now, AppCompat allows you to useandroid:theme
for Toolbars (deprecating theapp:theme
used previously) and, even better, bringsandroid:theme
support to all views on API 11+ devices.
换句话说:您现在可以在每个 View 的基础上应用主题,这使得解决原始问题变得更加容易:只需指定您要为相关 View 应用的主题。 IE。在原始问题的上下文中,比较以下结果:
<CheckBox
...
android:theme="@android:style/Theme.Holo" />
<CheckBox
...
android:theme="@android:style/Theme.Holo.Light" />
第一个
CheckBox
样式就像在深色主题中使用一样,第二个就像在浅色主题中使用一样,无论为您的 Activity 或应用程序设置的实际主题如何。当然,您不应再使用 Holo 主题,而应使用 Material。
关于Android 复选框样式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10135499/