在编程的时候,我们经常会在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
Also can you set styles to individual widgets if you set a style to the application? |
|||
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
As you can see, both are empty declarations that simply wrap
This style references both a background and button drawable. As
So this where the real magic happens: based on the theme's Now, if you want to change your As for your second question:
Absolutely, and they will override any activity- or application-set styles.
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:
The first line will actually return the resource id of the 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 |
||||
Maybe this will satisfies you - something.xml
Selector
For, the reference just refer |
|||
Perhaps you want something like:
Note, the textColor item. |
|||
In the previous answer also in the section
|
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
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