Android复选框样式 [英] Android checkbox style

查看:257
本文介绍了Android复选框样式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新的android和我试图设置一个样式到所有复选框在我的应用程序。我的应用程序样式设置为Theme.Holo是黑暗的,我想在我的列表视图的复选框是Theme.Holo.Light的样式。我不是想创建一个自定义风格。下面的代码似乎不工作,什么都没有发生。我需要这样做,因为我的列表视图有一个轻的纸纹理,复选框和复选框文本是白色的,我想要黑暗。

 < style name =CustomActivityThemeparent =@ android:style / Theme.Holo> 
< item name =android:checkboxStyle> @ style / customCheckBoxStyle< / item>
< / style>

< style name =customCheckBoxStyleparent =@ android:style / Widget.Holo.Light.CompoundButton.CheckBox>
< / style>

如果为应用程序设置样式,还可以将样式设置为单个小部件吗?

解决方案

注意:使用Android支持库v22.1.0和目标API级别11及以上?向下滚动到上次更新。







我的应用程式样式设定为Theme.Holo是黑暗的,我会
像我的列表视图中的复选框的样式Theme.Holo.Light。
我没有尝试创建自定义样式。


首先可能不明白为什么系统表现出这种行为,但当你实际看到力学,你可以轻易地推断出来。让我们逐步介绍一下。



首先,让我们来看看 Widget.Holo.Light.CompoundButton.CheckBox 样式定义。为了使事情更加清晰,我还添加了常规(非轻)样式定义。

 < style name =Widget.Holo.Light.CompoundButton.CheckBoxparent =Widget.CompoundButton.CheckBox/> 

< style name =Widget.Holo.CompoundButton.CheckBoxparent =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 表示基于当前主题的某些属性(这对实现很重要) 复选框



由于 listChoiceIndicatorMultiple ,你会发现它的多个声明 - 每个主题一个(如果它从父主题继承,则为none)。这将如下所示(为了清楚起见省略了其他属性):

 < 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.Lightparent =Theme.Light>
< item name=listChoiceIndicatorMultiple> @android:drawable / btn_check_holo_light< / item>
...
< / style>

所以这里真正的魔法发生:基于主题的 listChoiceIndicatorMultiple 属性,确定 CheckBox 的实际外观。您现在看到的现象很容易解释:因为外观是基于主题的(而且不是基于样式,因为这只是一个空定义),并且您继承自 Theme.Holo ,您将总是获得与主题匹配的 CheckBox 外观。



现在,如果要将 CheckBox 的外观更改为Holo.Light版本,您需要获取这些资源的副本,将它们添加到您的本地



至于您的第二个问题:


如果您为应用程式设定样式,还可以为个别小工具设定样式吗?


当然,活动或应用程序集样式。


有任何方法可以将主题(包含图片的样式)设置为复选框
窗口小部件。 (...)是否仍然使用此选择器: ,事情只是得到了很多更容易!发布说明(我强调)的报价:


Lollipop添加了通过视图级别覆盖主题的功能 android:theme XML属性 - 对于诸如光线活动的黑暗动作条等事情非常有用。现在,AppCompat允许您为工具栏使用 android:theme (弃用以前使用的 app:theme ), strong>更好,为API 11+设备上的所有视图带来 android:theme 支持。


换句话说:您现在可以在每个视图基础上应用主题,这使得解决原始问题更容易:只需指定主题喜欢申请相关视图。也就是说在原始问题的上下文中,比较以下结果:

 < CheckBox 
...
android:theme =@ android:style / Theme.Holo/>

< CheckBox
...
android:theme =@ android:style / Theme.Holo.Light/>

第一个 CheckBox



当然,你不应再使用这个主题了。 Holo主题,而是使用Material。


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.

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

解决方案

Note: Using Android Support Library v22.1.0 and targeting API level 11 and up? Scroll down to the last update.


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.

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.

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

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

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

<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 not style-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.

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.

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.

However, a way to access system resources after all is by doing an id lookup by name. Consider the following code snippet:

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.

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


Update2:

With the introduction of Support Library v22.1.0, things have just gotten a lot easier! A quote from the release notes (my emphasis):

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 use android:theme for Toolbars (deprecating the app:theme used previously) and, even better, brings android:theme support to all views on API 11+ devices.

In other words: you can now apply a theme on a per-view basis, 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.

Of course you should no longer be using the Holo theme, but instead use Material.

这篇关于Android复选框样式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆