shape和selector是android ui设计中经常用到的,比如我们要自定义一个圆角button,点击button有些效果的变化,就要用到shape和selector。可以这样说,shape和selector在美化控件中的作用是至关重要的。
1.shape
简介
作用:xml中定义的几何形状
位置:res/drawable/文件的名称.xml
使用的方法:
java代码中:r.drawable.文件的名称
xml中:android:background="@drawable/文件的名称"
属性:
<shape> android:shape=["rectangle" | "oval" | "line" | "ring"]
其中rectagle矩形,oval椭圆,line水平直线,ring环形
<shape>中子节点的常用属性:
<gradient> 渐变
android:startcolor 起始颜色
android:endcolor 结束颜色
android:angle 渐变角度,0从上到下,90表示从左到右,数值为45的整数倍默认为0;
android:type 渐变的样式 liner线性渐变 radial环形渐变 sweep
<solid > 填充
android:color 填充的颜色
<stroke > 描边
android:width 描边的宽度
android:color 描边的颜色
android:dashwidth 表示'-'横线的宽度
android:dashgap 表示'-'横线之间的距离
<corners > 圆角
android:radius 圆角的半径 值越大角越圆
android:toprightradius 右上圆角半径
android:bottomleftradius 右下圆角角半径
android:topleftradius 左上圆角半径
android:bottomrightradius 左下圆角半径
2.selector
简介
位置:res/drawable/文件的名称.xml
使用的方法:
java代码中:r.drawable.文件的名称
xml中:android:background="@drawable/文件的名称"
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
|
<?xml version= "1.0" encoding= "utf-8" ?>
<selector xmlns:android= "http://schemas.android.com/apk/res/android" >
<!-- 默认时的背景图片-->
<item android:drawable= "@drawable/pic1" />
<!-- 没有焦点时的背景图片 -->
<item
android:state_window_focused= "false"
android:drawable= "@drawable/pic_blue"
/>
<!-- 非触摸模式下获得焦点并单击时的背景图片 -->
<item
android:state_focused= "true"
android:state_pressed= "true"
android:drawable= "@drawable/pic_red"
/>
<!-- 触摸模式下单击时的背景图片-->
<item
android:state_focused= "false"
android:state_pressed= "true"
android:drawable= "@drawable/pic_pink"
/>
<!--选中时的图片背景-->
<item
android:state_selected= "true"
android:drawable= "@drawable/pic_orange"
/>
<!--获得焦点时的图片背景-->
<item
android:state_focused= "true"
android:drawable= "@drawable/pic_green"
/>
</selector>
|
第一个例子:圆角的button
http://liangruijun.blog.51cto.com/3061169/630051
第二个例子:shape+selector综合使用的例子 漂亮的listview
先看看这个例子的结构:
selector.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
|
<?xml version= "1.0" encoding= "utf-8" ?>
<selector xmlns:android= "http://schemas.android.com/apk/res/android" >
<item android:state_selected= "true" >
<shape>
<gradient android:angle= "270" android:endcolor= "#99bd4c"
android:startcolor= "#a5d245" />
<size android:height= "60dp" android:width= "320dp" />
<corners android:radius= "8dp" />
</shape>
</item>
<item android:state_pressed= "true" >
<shape>
<gradient android:angle= "270" android:endcolor= "#99bd4c"
android:startcolor= "#a5d245" />
<size android:height= "60dp" android:width= "320dp" />
<corners android:radius= "8dp" />
</shape>
</item>
<item>
<shape>
<gradient android:angle= "270" android:endcolor= "#a8c3b0"
android:startcolor= "#c6cfce" />
<size android:height= "60dp" android:width= "320dp" />
<corners android:radius= "8dp" />
</shape>
</item>
</selector>
|
list_item.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
|
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "horizontal"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:background= "@drawable/selector"
>
<imageview
android:id= "@+id/img"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_gravity= "center_vertical"
android:layout_marginleft= "20dp"
/>
<textview
android:text= "data"
android:id= "@+id/title"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:gravity= "center_vertical"
android:layout_marginleft= "20dp"
android:layout_margintop= "20dp"
android:textsize= "14sp"
android:textstyle= "bold"
android:textcolor= "@color/black"
>
</textview>
</linearlayout>
|
main.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<?xml version= "1.0" encoding= "utf-8" ?>
<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android"
android:orientation= "vertical"
android:layout_width= "fill_parent"
android:layout_height= "wrap_content"
android:background= "#253853"
>
<listview
android:id= "@+id/list"
android:layout_width= "match_parent"
android:layout_height= "match_parent"
android:cachecolorhint= "#00000000"
android:divider= "#2a4562"
android:dividerheight= "3px"
android:listselector= "#264365"
android:drawselectorontop= "false"
>
</listview>
</linearlayout>
|
colors.xml
1
2
3
4
5
6
7
8
|
<?xml version= "1.0" encoding= "utf-8" ?>
<resources>
<color name= "white" >#ffffffff</color>
<color name= "transparency" ># 00000000 </color>
<color name= "title_bg" >#1c86ee</color>
<color name= "end_color" >#a0cfef83</color>
<color name= "black" ># 464646 </color>
</resources>
|
mainactivity.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
|
package com.lingdududu.customlist;
import java.util.arraylist;
import java.util.hashmap;
import xb.customlist.r;
import android.r.array;
import android.app.activity;
import android.os.bundle;
import android.widget.arrayadapter;
import android.widget.listview;
import android.widget.simpleadapter;
public class mainactivity extends activity {
listview list;
string data[] = new string[]{
"china" , "uk" , "usa" , "japan" , "german" , "canada" , "et" , "narotu"
};
/** called when the activity is first created. */
@override
public void oncreate(bundle savedinstancestate) {
super .oncreate(savedinstancestate);
setcontentview(r.layout.main);
list =(listview) findviewbyid(r.id.list);
simpleadapter adapter = new simpleadapter( this , getdata(), r.layout.list_item,
new string[]{ "title" , "img" }, new int []{r.id.title,r.id.img});
list.setadapter(adapter);
}
private arraylist<hashmap<string, object>> getdata() {
arraylist<hashmap<string, object>> dlist = new arraylist<hashmap<string,object>>();
for ( int i = 0 ;i<data.length;i++){
hashmap<string, object>map = new hashmap<string, object>();
map.put( "title" , data[i]);
map.put( "img" , r.drawable.item_left2);
dlist.add(map);
}
return dlist;
}
}
|
效果图:
以上所述是小编给大家分享的android开发教程之shape和selector的结合使用的相关内容,希望对大家有所帮助。