Android 复选框样式
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10135499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android checkbox style
提问by user1330739
I am new to android and I'm trying to set a style to all check boxes in my application. 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. I need to do this because my list view has a light paper texture and the check box and check box text is white which i would like dark.
我是 android 新手,我正在尝试为应用程序中的所有复选框设置样式。我的应用程序样式设置为深色的 Theme.Holo,我希望列表视图中的复选框为 Theme.Holo.Light 样式。我不是要创建自定义样式。下面的代码似乎不起作用,根本没有任何反应。我需要这样做是因为我的列表视图有一个浅纸纹理,复选框和复选框文本是白色的,我想要黑色。
<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>
Also can you set styles to individual widgets if you set a style to the application?
如果您为应用程序设置样式,您还可以为单个小部件设置样式吗?
回答by MH.
Note: Using Android Support Library v22.1.0 and targeting API level 11 and up? Scroll down to the last update.
注意:使用 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.
我的应用程序样式设置为深色的 Theme.Holo,我希望列表视图中的复选框为 Theme.Holo.Light 样式。我不是要创建自定义样式。下面的代码似乎不起作用,根本没有任何反应。
At first it may not be apparent why the system exhibits this behaviour, but when you actually look into the mechanics you can easily deduce it. Let me take you through it step by step.
起初,系统为什么会表现出这种行为可能并不明显,但是当您真正研究机制时,您可以轻松推断出它。让我一步一步带你完成它。
First, let's take a look what the Widget.Holo.Light.CompoundButton.CheckBox
style defines. To make things more clear, I've also added the 'regular' (non-light) style definition.
首先,让我们看看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" />
As you can see, both are empty declarations that simply wrap Widget.CompoundButton.CheckBox
in a different name. So let's look at that parent style.
如您所见,两者都是空声明,只是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>
This style references both a background and button drawable. btn_check_label_background
is simply a 9-patch and hence not very interesting with respect to this matter. However, ?android:attr/listChoiceIndicatorMultiple
indicates that some attribute based on the current theme(this is important to realise) will determine the actual look of the CheckBox
.
此样式同时引用背景和按钮可绘制。btn_check_label_background
只是一个 9 补丁,因此在这个问题上不是很有趣。但是,?android:attr/listChoiceIndicatorMultiple
表示基于当前主题的某些属性(意识到这一点很重要)将决定CheckBox
.
As listChoiceIndicatorMultiple
is a theme attribute, you will find multiple declarations for it - one for each theme (or none if it gets inherited from a parent theme). This will look as follows (with other attributes omitted for clarity):
作为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>
So this where the real magic happens: based on the theme's listChoiceIndicatorMultiple
attribute, the actual appearance of the CheckBox
is determined. The phenomenon you're seeing is now easily explained: since the appearance is theme-based (and notstyle-based, because that is merely an empty definition) and you're inheriting from Theme.Holo
, you will always get the CheckBox
appearance matching the theme.
所以这就是真正的魔法发生的地方:根据主题的listChoiceIndicatorMultiple
属性,CheckBox
确定的实际外观。您看到的现象现在很容易解释:由于外观是基于主题的(而不是基于样式的,因为这只是一个空定义)并且您是从 继承的Theme.Holo
,因此您将始终获得CheckBox
与主题匹配的外观。
Now, if you want to change your CheckBox
's appearance to the Holo.Light version, you will need to take a copy of those resources, add them to your local assets and use a custom style to apply them.
现在,如果要将CheckBox
的外观更改为 Holo.Light 版本,则需要复制这些资源,将它们添加到本地资产并使用自定义样式来应用它们。
As for your second question:
至于你的第二个问题:
Also can you set styles to individual widgets if you set a style to the application?
如果您为应用程序设置样式,您还可以为单个小部件设置样式吗?
Absolutely, and they will override any activity- or application-set styles.
当然,它们将覆盖任何活动或应用程序集样式。
Is there any way to set a theme(style with images) to the checkbox widget. (...) Is there anyway to use this selector: link?
有什么方法可以为复选框小部件设置主题(带图像的样式)。(...) 无论如何要使用这个选择器:链接?
Update:
更新:
Let me start with saying again that you're not supposed to rely on Android's internal resources. There's a reason you can't just access the internal namespace as you please.
首先让我再说一遍,你不应该依赖 Android 的内部资源。您不能随意访问内部命名空间是有原因的。
However, a way to access system resources after all is by doing an id lookup by name. Consider the following code snippet:
但是,毕竟访问系统资源的一种方法是按名称进行 id 查找。考虑以下代码片段:
int id = Resources.getSystem().getIdentifier("btn_check_holo_light", "drawable", "android");
((CheckBox) findViewById(R.id.checkbox)).setButtonDrawable(id);
The first line will actually return the resource id of the btn_check_holo_light
drawable resource. Since we established earlier that this is the button selector that determines the look of the CheckBox
, we can set it as 'button drawable' on the widget. The result is a CheckBox
with the appearance of the Holo.Light
version, no matter what theme/style you set on the application, activity or widget in xml. Since this sets only the button drawable, you will need to manually change other styling; e.g. with respect to the text appearance.
第一行实际上将返回btn_check_holo_light
可绘制资源的资源 ID 。由于我们之前确定这是决定 外观的按钮选择器CheckBox
,因此我们可以在小部件上将其设置为“按钮可绘制”。结果是一个CheckBox
具有Holo.Light
版本外观的,无论您在 xml 中的应用程序、活动或小部件上设置什么主题/样式。由于这仅设置按钮可绘制,因此您需要手动更改其他样式;例如,关于文本外观。
Below a screenshot showing the result. The top checkbox uses the method described above (I manually set the text colour to black in xml), while the second uses the default theme-based Holo
styling (non-light, that is).
下面是显示结果的屏幕截图。顶部复选框使用上述方法(我在 xml 中手动将文本颜色设置为黑色),而第二个使用基于主题的默认Holo
样式(即非浅色)。
Update2:
更新2:
With the introduction of Support Library v22.1.0, things have just gotten a lot easier! A quote from the release notes (my emphasis):
随着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.
Lollipop 添加了通过使用
android:theme
XML 属性在视图级别覆盖主题的功能- 对于诸如浅色活动上的深色动作条之类的东西非常有用。现在,AppCompat 允许您使用android:theme
工具栏(弃用app:theme
以前使用的),甚至更好地android:theme
支持 API 11+ 设备上的所有视图。
In other words: you can now apply a theme on a per-viewbasis, which makes solving the original problem a lot easier: just specify the theme you'd like to apply for the relevant view. I.e. in the context of the original question, compare the results of below:
换句话说:您现在可以在每个视图的基础上应用一个主题,这使得解决原始问题变得更加容易:只需指定您要为相关视图应用的主题。即在原始问题的上下文中,比较以下结果:
<CheckBox
...
android:theme="@android:style/Theme.Holo" />
<CheckBox
...
android:theme="@android:style/Theme.Holo.Light" />
The first CheckBox
is styled as if used in a dark theme, the second as if in a light theme, regardless of the actual theme set to your activity or application.
第一个CheckBox
样式就像在深色主题中使用一样,第二个在浅色主题中使用,而不管为您的活动或应用程序设置的实际主题如何。
Of course you should no longer be using the Holo theme, but instead use Material.
当然,您不应再使用 Holo 主题,而应使用 Material。
回答by Praveenkumar
Maybe this will satisfy you -
也许这会让你满意——
something.xml
东西.xml
<CheckBox
android:text="Custom CheckBox"
android:button="@drawable/checkbox_selector"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
Selector
选择器
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/star_down" />
<item android:state_checked="false" android:drawable="@drawable/star" />
</selector>
For, the reference just refer here
对于,参考只是指here
回答by Rémy DAVID
The correct way to do it for Material design is :
为材料设计做的正确方法是:
Style :
风格 :
<style name="MyCheckBox" parent="Theme.AppCompat.Light">
<item name="colorControlNormal">@color/foo</item>
<item name="colorControlActivated">@color/bar</item>
</style>
Layout :
布局 :
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Check Box"
android:theme="@style/MyCheckBox"/>
It will preserve Material animations on Lollipop+.
它将保留 Lollipop+ 上的 Material 动画。
回答by Edwin Evans
Perhaps you want something like:
也许你想要这样的东西:
<style name="CustomActivityTheme" parent="@android:style/Theme.Holo">
<item name="android:checkboxStyle">@style/customCheckBoxStyle</item>
</style>
<style name="customCheckBoxStyle" parent="@android:style/Widget.CompoundButton.CheckBox">
<item name="android:textColor">@android:color/black</item>
</style>
Note, the textColor item.
请注意, textColor 项。
回答by Alex Che
In the previous answer also in the section <selector>...</selector>
you may need:
在<selector>...</selector>
您可能需要的部分中的上一个答案中:
<item android:state_pressed="true" android:drawable="@drawable/checkbox_pressed" ></item>