Android开发教程之shape和selector的结合使用

shape和selector是Android UI设计中经常用到的,比如我们要自定义一个圆角Button,点击Button有些效果的变化,就要用到shape和selector。可以这样说,shape和selector在美化控件中的作用是至关重要的。

1.Shape

简介

作用:XML中定义的几何形状

位置:res/drawable/文件的名称.xml

使用的方法:

Java代码中:R.drawable.文件的名称

XML中:android:background="@drawable/文件的名称"

属性:

android:shape=["rectangle" | "oval" | "line" | "ring"]

其中rectagle矩形,oval椭圆,line水平直线,ring环形

中子节点的常用属性:

渐变

android:startColor 起始颜色
android:endColor 结束颜色
android:angle 渐变角度,0从上到下,90表示从左到右,数值为45的整数倍默认为0;
android:type 渐变的样式 liner线性渐变 radial环形渐变 sweep
填充
android:color 填充的颜色
描边
android:width 描边的宽度
android:color 描边的颜色
android:dashWidth 表示'-'横线的宽度
android:dashGap 表示'-'横线之间的距离
圆角
android:radius 圆角的半径 值越大角越圆
android:topRightRadius 右上圆角半径

android:bottomLeftRadius 右下圆角角半径
android:topLeftRadius 左上圆角半径
android:bottomRightRadius 左下圆角半径

2.Selector

简介

位置:res/drawable/文件的名称.xml

使用的方法:

Java代码中:R.drawable.文件的名称

XML中:android:background="@drawable/文件的名称"

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

第一个例子:圆角的Button

http://liangruijun.blog.51cto.com/3061169/630051

第二个例子:shape+selector综合使用的例子 漂亮的ListView

先看看这个例子的结构:

Android开发教程之shape和selector的结合使用_第1张图片

selector.xml

 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

list_item.xml

 
 
 
 
 

main.xml

 
 
 
 
 

colors.xml

 
 
#FFFFFFFF 
#00000000 
#1C86EE 
#A0cfef83 
#464646 
 

MainActivity.xml

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> getData() { 
ArrayList> dlist = new ArrayList>(); 
for(int i =0;imap = new HashMap(); 
map.put("title", data[i]); 
map.put("img", R.drawable.item_left2); 
dlist.add(map); 
} 
return dlist; 
} 
}

效果图:

Android开发教程之shape和selector的结合使用_第2张图片

以上所述是小编给大家分享的Android开发教程之shape和selector的结合使用的相关内容,希望对大家有所帮助。

你可能感兴趣的:(Android开发教程之shape和selector的结合使用)