android杂记7:drawable state各个属性详解
http://blog.csdn.net/leasystu/article/details/7250885
我们在定义一个drawable的时候可以通过xml定义的drawable对象。它使得一个图片能在不同的状态下显示不同的图案,比如一个Button,它有pressed,focused,或者其它状态,通过使用state list drawable,你就可以为每种状态提供不同的图片。
先看一个范例:
XML file saved at res/drawable/button.xml:
[java] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:state_enabled="true" android:state_window_focused="false"
android:drawable="@drawable/button_pressed" /> <!-- pressed,enable等多个属性 -->
<item android:state_focused="true"
android:drawable="@drawable/button_focused" /> <!-- focused -->
<item android:state_hovered="true"
android:drawable="@drawable/button_focused" /> <!-- hovered -->
<item android:drawable="@drawable/button_normal" /> <!-- default -->
</selector>
This layout XML applies the state list drawable to a Button:
[java] view plaincopy
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:background="@drawable/button" />
android:drawable 放一个drawable资源
android:state_pressed 是否按下,如一个按钮触摸或者点击。
android:state_focused 是否取得焦点,比如用户选择了一个文本框。
android:state_hovered 光标是否悬停,通常与focused state相同,它是4.0的新特性
android:state_selected 被选中,它与focus state并不完全一样,如一个list view 被选中的时候,它里面的各个子组件可能通过方向键,被选中了。
android:state_checkable 组件是否能被check。如:RadioButton是可以被check的。
android:state_checked 被checked了,如:一个RadioButton可以被check了。
android:state_enabled 能够接受触摸或者点击事件
android:state_activated 被激活(这个麻烦举个例子,不是特明白)
android:state_window_focused 应用程序是否在前台,当有通知栏被拉下来或者一个对话框弹出的时候应用程序就不在前台了
注意:如果有多个item,那么程序将自动从上到下进行匹配,最先匹配的将得到应用。(不是通过最佳匹配)
如果一个item没有任何的状态说明,那么它将可以被任何一个状态匹配。
详细的请看官方的API,那里写的更详细~!
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Android开发技巧不同状态的Button
http://blog.csdn.net/sjf0115/article/details/7333895
使用XML实现按钮改变焦点设置背景图,获得焦点时,获得焦点并按下,失去焦点时,默认时...
新建res/drawable/button.xml
[java] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- 获得焦点时 -->
<item
android:state_focused="true"
android:state_pressed="false"
android:drawable="@drawable/focusimage"
/>
<!-- 获得焦点并按下 -->
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="@drawable/clickimage"
/>
<!-- 失去焦点时 -->
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="@drawable/clickimage"
/>
<!-- 默认时 -->
<item android:drawable="@drawable/normal"/>
</selector>
main.xml
[java] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:layout_marginTop="20dp"
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="不同状态的Button"
android:background="@drawable/button"/>
</LinearLayout>
Android中按钮样式的设定(按下,焦点和正常状态)
http://blog.sina.com.cn/s/blog_49cb313d01014pef.html
首先写一个定义Button样式的XML文件:
新建Android XML文件,类型选Drawable,根结点选selector,文件名就buton_style吧。
程序自动给我们刚刚建的文件里加了selector结点,我们只需要在selector结点里写上三种状态时显示的背景图片(按下、获取焦点,正常)。
代码如下:
Xml代码
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@drawable/play_press" />
<item android:state_focused="true" android:drawable="@drawable/play_press" />
<item android:drawable="@drawable/play" />
</selector>
我这里获取焦点跟点击时显示的是同一张图片,必须严格照上面的顺序写,不可倒。
接下来只要在布局时写Button控件时应用到Button的Background属性即可。
Xml代码
<Button android:id="@+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:background="@drawable/button_style"
>
</Button>
android 自定义 radiobutton 文字颜色随选中状态而改变
http://blog.csdn.net/hpccn/article/details/7591662
主要是写一个 color selector
在res/建一个文件夹取名color
res/color/color_radiobutton.xml
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:color="@color/color_text_selected"/>
<!-- not selected -->
<item android:color="@color/color_text_normal"/>
</selector>
程序中就可以直接使用了
[html] view plaincopy
//layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RadioGroup
android:id="@+id/radiogroup_personal_condition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radiobutton_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector_radio"
android:button="@null"
android:checked="true"
android:gravity="center"
android:text="目录"
android:textColor="@color/color_radiobutton"
android:textSize="@dimen/font_size"
android:textStyle="bold" />
<RadioButton
android:id="@+id/radiobutton_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/selector_radio"
android:button="@null"
android:gravity="center"
android:text="书签"
android:textColor="@color/color_radiobutton"
android:textSize="@dimen/font_size"
android:textStyle="bold" />
</RadioGroup>
</LinearLayout>
android杂记7:drawable state各个属性详解
最新推荐文章于 2023-06-30 14:20:01 发布