一。网络资源案例(关于selector用法):android背景选择器selector用法汇总(2011-04-19
13:40:00)
1.创建xml文件,位置:drawable/xxx.xml,同目录下记得要放相关图片
version="1.0" encoding="utf-8" ?>
xmlns:android="http://schemas.android.com/apk/res/android">
/>
android:drawable="@drawable/pic1" />
android:state_pressed="true" android:drawable=
"@drawable/pic2" />
android:state_focused="false" android:state_pressed="true"
android:drawable="@drawable/pic3"
/>
android:drawable="@drawable/pic4"
/>
android:drawable="@drawable/pic5"
/>
2.使用xml文件:
a.方法一:在listview中配置android:listSelector="@drawable/xxx
或者在listview的item中添加属性android:background="@drawable/xxx"
b.方法二:Drawable
drawable = getResources().getDrawable(R.drawable.xxx);
ListView.setSelector(drawable);但是这样会出现列表有时候为黑的情况,需要加上:android:cacheColorHint="@android:color/transparent"使其透明。
相关属性:
android:state_selected是选中
android:state_focused是获得焦点
android:state_pressed是点击
android:state_enabled是设置是否响应事件,指所有事件
根据这些状态同样可以设置button的selector效果。也可以设置selector改变button中的文字状态。
以下是配置button中的文字效果:
drawable/button_font.xml
version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android">
android:state_selected="true" android:color="#FFF"
/>
android:state_focused="true" android:color="#FFF"
/>
android:state_pressed="true" android:color="#FFF"
/>
/>
Button还可以实现更复杂的效果,例如渐变
drawable/button_color.xml
version="1.0" encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android">
/
android:state_pressed="true">//定义当button 处于pressed
状态时的形态。
android:startColor="#8600ff"
/>
/>
android:radius="5dp" />
android:left="10dp"
android:top="10dp"
android:bottom="10dp" android:right="10dp"/>
android:state_focused="true">//定义当button获得
focus时的形态
android:startColor="#eac100"/>
android:width="2dp" android:color="#333333"
color="#ffffff"/>
/>
android:top="10dp"
android:bottom="10dp"
android:right="10dp"/>
3.需要在包含
button的xml文件里添加两项。例如main.xml 文件,需要在
/>里加两项android:focusable="true"
android:background="@drawable/button_color"
二。shape画图:
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
android:endColor="#c0000000"
android:angle="90"
/>
android:dashGap="2dp"
android:width="2dp"
android:color="#FF00ff00">
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"
/>
三。黄色边框Edittext背景:android:background="@android:drawable/editbox_background"
四。android官网帮助文档中layer_list(层叠列表)的用法:
1.所有属性:
encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
>
android:id="@[+][package:]id/resource_name"
android:bottom="dimension"
android:drawable="@[package:]drawable/drawable_resource"
android:left="dimension"
android:right="dimension"
android:top="dimension"/>
2.案例
a. drawable中新建XML:
xmlns:android="http://schemas.android.com/apk/res/android">
android:src="@drawable/android_red"
android:gravity="center" />
android:top="10dp" android:left="10dp">
android:src="@drawable/android_green"
android:gravity="center" />
android:top="20dp" android:left="20dp">
android:src="@drawable/android_blue"
android:gravity="center" />
b. layout布局文件中的XML:
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/layers" />
这样就实现了几个图片源文件的层叠效果,主要用于图像拼合
五。android官网帮助文档中selector(分别对控件在不同状态下的显示效果)的用法
1.所有属性
encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:constantSize=["true" | "false"]
android:dither=["true" |
"false"]
android:variablePadding=["true" | "false"] >
android:drawable="@[package:]drawable/drawable_resource"
android:state_pressed=["true"
| "false"]
android:state_focused=["true"
| "false"]
android:state_hovered=["true"
| "false"]
android:state_selected=["true"
| "false"]
android:state_checkable=["true" | "false"]
android:state_checked=["true"
| "false"]
android:state_enabled=["true"
| "false"]
android:state_activated=["true" | "false"]
android:state_window_focused=["true" | "false"]
/>
2.案例
a。drawable中新建的button.xml:
encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android">
android:state_pressed="true"
android:drawable="@drawable/button_pressed" />
android:drawable="@drawable/button_focused" />
android:state_hovered="true"
android:drawable="@drawable/button_focused"
/>
android:drawable="@drawable/button_normal" />
b、layout文件中的控件的属性中需加入
android:background="@drawable/button"
六。android官网帮助文档中Transition
Drawable(实现控件淡入淡出)的用法
1. 所有属性
encoding="utf-8"?>
>
android:drawable="@[package:]drawable/drawable_resource"
android:id="@[+][package:]id/resource_name"
android:top="dimension"
android:right="dimension"
android:bottom="dimension"
android:left="dimension"
/>
2. 案例
a。drawable中新建transition.xml:
encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android">
android:drawable="@drawable/on" />
android:drawable="@drawable/off" />
b。在layout布局文件的对应控件设置:
android:id="@+id/button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:src="@drawable/transition" />
c。在Java代码中的实现对应控件的淡入淡出效果:
ImageButton button =
(ImageButton) findViewById(R.id.button);TransitionDrawable drawable
= (TransitionDrawable)
button.getDrawable();drawable.startTransition(500);
startTransition(500)表示的是从transition.xml文件中的实现从上一项到下一项的转变所用的时间。
七。android官网帮助文档中Clip
Drawable(实现剪辑图片资源)的用法
1.所有属性
encoding="utf-8"?>
android:drawable="@drawable/drawable_resource"
android:clipOrientation=["horizontal" |
"vertical"]
android:gravity=["top" |
"bottom" | "left" | "right" | "center_vertical" | "fill_vertical" |
"center_horizontal" | "fill_horizontal" | "center" | "fill" |
"clip_vertical" | "clip_horizontal"] />
2.案例
a。drawable中新建clip.xml:
encoding="utf-8"?>
xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/android"
android:clipOrientation="horizontal"
android:gravity="left"
/>
b。layout布局文件对应控件设置:
android:id="@+id/image"
android:background="@drawable/clip"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
c。Java代码中实现对应控件的剪辑效果:
ImageView imageview =
(ImageView) findViewById(R.id.image);ClipDrawable drawable =
(ClipDrawable)
imageview.getDrawable();drawable.setLevel(drawable.getLevel() +
1000);
setlevel():设置剪切的增量,默认为0,这时表示的是剪切整个控件,当等于10000时,剪切量为0,所以显示整个控件。