LinearLayout中有关CheckBox的处理
来源:互联网 发布:淘宝店铺专享红包 编辑:程序博客网 时间:2024/06/16 19:13
在编程的时候,我们经常会在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
Android checkbox style
![](http://static.adzerk.net/Advertisers/46a9844d6e504212a85bc72ddd7dd829.png)
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiY20iOjg0NywiY2giOjExNzgsImNyIjo1OTIzLCJkaSI6Ijk5ZDJhNGE3OTA3YzRjYzU4ZjkwMWEwYThmYWQ0ODcxIiwiZG0iOjEsImZjIjo4ODA5LCJmbCI6MjQ0NCwia3ciOiJhbmRyb2lkLGNoZWNrYm94LHN0eWxlcyx0aGVtZXMiLCJudyI6MjIsInJmIjoiaHR0cDovL3d3dy5nb29nbGUuY29tLmhrL3VybD9zYT10IiwicnYiOjAsInByIjoxNTY4LCJzdCI6ODI3Nywiem4iOjQzfQ&s=LM2wvH5d4LsJFb8rszr3DubtNRY)
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?
![](http://i.stack.imgur.com/tKsDb.png)
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 theCheckBox
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).
![](http://static.adzerk.net/Advertisers/d31c8d7d3cf640a6b29d9ad71788dd4a.png)
![](http://engine.adzerk.net/i.gif?e=eyJhdiI6NDE0LCJhdCI6NCwiY20iOjI5MTEsImNoIjoxMTc4LCJjciI6NTkxNiwiZGkiOiI0MDQ0Y2Y1ZTE5OGI0NTk5YmY0YTAwZWNjODk4ZDQzZCIsImRtIjoxLCJmYyI6MTAxODEsImZsIjo3NDcyLCJrdyI6ImFuZHJvaWQsY2hlY2tib3gsc3R5bGVzLHRoZW1lcyIsIm53IjoyMiwicmYiOiJodHRwOi8vd3d3Lmdvb2dsZS5jb20uaGsvdXJsP3NhPXQiLCJydiI6MCwicHIiOjE2MDQsInN0Ijo4Mjc3LCJ6biI6NDR9&s=_EwpCEfFR84_Yzl0MoMO7Ty1QqY)
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
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.
In the previous answer also in the section <selector>...</selector>
you may need:
<item android:state_pressed="true" android:drawable="@drawable/checkbox_pressed" ></item>
- LinearLayout中有关CheckBox的处理
- 有关input按钮中checkbox的复选提交前端处理学习总结
- 有关Java Web 中CheckBox提交的问题
- android Linearlayout中有关gravity与layout_gravity对布局的影响
- SQLite中有关datetime字段的处理
- 有关ios中图片的处理
- java中有关异常处理的问题
- Giraph中有关Aggretor的处理过程
- js中checkbox处理,js获得checkbox
- 在JSP页面中,对同名的CHECKBOX的处理
- LinearLayout中weight的学习
- LinearLayout中addView的使用
- LinearLayout中layout_weight的理解
- android中典型的LinearLayout
- django 中如何处理checkbox
- AspxGrid模板列中Checkbox 值在客户端的处理
- Android 中如何处理checkbox的选中、未选中
- andorid 中带checkbox的listView问题处理
- Maven SpringMVC项目基本搭建(备忘)
- 骑士人才系统后台用户名密码重设工具源码
- input子系统的基础知识
- wii homebrew 程序编程入门指南
- 黑马程序员:DVD管理系统项目之四--初识图形用户界面
- LinearLayout中有关CheckBox的处理
- fopen参数说明
- ffmpeg移植改接口调用 |
- CF 316C2(Tidying Up-二分图最大边权)
- Hibernate4.2.2使用Annotation配置
- 从程序员到项目经理(7):程序员加油站 -- 完美主义也是一种错
- 快速排序(C.A.R.Hoare版)
- apple的一个专利
- ok6410学习笔记(11.kobject学习记录)