现在的位置: 首页 > 综合 > 正文

LinearLayout中有关CheckBox的处理

2013年10月14日 ⁄ 综合 ⁄ 共 8268字 ⁄ 字号 评论关闭

在编程的时候,我们经常会在LinearLayout上面添加一个CheckBox

并且将该LinearLayout和CheckBox作为一个完整的选项。

我们可以如下定义该.xml布局文件(这里设为layout_checkbox.xml吧)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:orientation="horizontal"
    android:descendantFocusability="blocksDescendants" >

    <CheckBox
        android:id="@+id/ckbSelect"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
       android:button="@drawable/btn_check_holo_light"
       android:clickable="false"/>

    <TextView
        android:id="@+id/tvName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="name" />
</LinearLayout>



看到没有,这里有3段标红的代码需要我们好好考虑。。。

android:descendantFocusability="blocksDescendants"

这段表示,CheckBox以及TextView的焦点都交给Layout处理。

android:button="@drawable/btn_check_holo_light"

这段表示当你theme的CheckBox的Style不是holo_light,而你又想用holo.light的时候,就需要将

AndroidSDK/platforms/android-17/data/res/drawable/btn_check_holo_light.xml拷贝到你的drawable文件里面

然后可选择性的将btn_check_holo_light.xml中引用到的图片从AndroidSDK/platforms/android-17/data/res/drawable-ldpi或者

AndroidSDK/platforms/android-17/data/res/drawable-mdpi或者AndroidSDK/platforms/android-17/data/res/drawable-hdpi

拷贝出来,这里每个文件夹中的图片名都一样,只是尺寸不同而已。。。。。

android:clickable="false"

是当你将Layout作为ListView某一项的时候,确保ListView可以响应OnItemClickListener事件而已。。。。

除此之外,还有一种方法:

就是直接获取android系统资源文件,方法如下:

int id = Resources.getSystem().getIdentifier("btn_check_holo_light", "drawable", "android");
((CheckBox) findViewById(R.id.checkbox)).setButtonDrawable(id);

这样达成的效果和上面将系统的资源文件拷贝出来的结果一样。。。。

这些文章只能在老外那么找,,,。。。。。

另外再转载一篇对我帮助很大的文章,它来之stackoverflow

地址:http://stackoverflow.com/questions/10135499/android-checkbox-style

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?

share|improve
this question
   

4 Answers

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 thecurrent theme (this is important to realise) will determine the actual look of theCheckBox.

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 theCheckBox is determined. The phenomenon you're seeing is now easily explained: since the appearance is
theme-based (andnot style-based, because that is merely an empty definition) and you're inheriting fromTheme.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 theCheckBox, we can set it as 'button drawable'
on the widget. The result is aCheckBox 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-basedHolo styling (non-light, that is).

Screenshot showing the result

share|improve
this answer
 
Thank you for taking the time to respond. I see the issue here and I'm still wondering if this can be done with out me having to do a lot of work. Is there any way to set a theme(style with images) to the checkbox widget. I know
that I can find the images and include them in my project but it seems like over kill. Is there anyway to use this selector:github.com/android/platform_frameworks_base/blob/master/core/…

– user1330739Apr
16 '12 at 5:53
1  
@user1330739: Yes and no. You're not supposed to rely on internal resources as Google may decide to change them in future releases. Including a copy of the drawables in your local resources is really your best option. That being
said, there is another (ugly) solution I'm aware of, but that only works at runtime (meaning you cannot reference it in your xml files). Please see the edit to my answer above.
– MH.Apr
16 '12 at 10:54

Maybe this will satisfies you -

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

share|improve
this answer
   

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.

share|improve
this answer
   

In the previous answer also in the section <selector>...</selector> you may need:

<item android:state_pressed="true" android:drawable="@drawable/checkbox_pressed" ></item>
share|improve
this answer

抱歉!评论已关闭.