这几天十一放假,在群里非常活跃,很多朋友问如何实现android中listview的圆角功能,像Iphone设置里面的tableView如 如下效果:

实现过程
其实这个功能实现也很简单,只是很多朋友没有仔细的去了解android布局的相关知识,这里我们使用了android中的shade的圆角功能来实现的。
java代码很简单,就一个activity,一个listview。listview中要判断item的位置,第一条,最后一条和中间的item是不一样的。代码如下:
java代码和布局文件
AndroidlistviewActivity.java
01 | package com.yangfuhai.listviewdemo; |
03 | import android.app.Activity; |
04 | import android.os.Bundle; |
05 | import android.view.View; |
06 | import android.widget.ListView; |
08 | * @title 圆角listview的实现 |
09 | * @description 圆角listview的实现 |
10 | * @company 探索者网络工作室(www.tsz.net) |
11 | * <a href="http://my.oschina.net/arthor" class="referer" target="_blank">@author</a> michael Young (www.YangFuhai.com) |
15 | public class AndroidlistviewActivity extends Activity { |
17 | ListViewAdapter mListViewAdapter; |
20 | public void onCreate(Bundle savedInstanceState) { |
21 | super .onCreate(savedInstanceState); |
22 | setContentView(R.layout.main); |
23 | mListView = (ListView) findViewById(R.id.listview); |
24 | mListViewAdapter = new ListViewAdapter( this ); |
25 | mListView.setAdapter(mListViewAdapter); |
33 | public void add(View view) { |
34 | mListViewAdapter.addData( " ----item --- " +i+ " ---" ); |
35 | mListViewAdapter.notifyDataSetChanged(); |
43 | public void del(View view) { |
44 | mListViewAdapter.delData(); |
45 | mListViewAdapter.notifyDataSetChanged(); |
适配器 ListViewAdapter.java
01 | package com.yangfuhai.listviewdemo; |
03 | import java.util.ArrayList; |
04 | import java.util.List; |
06 | import android.content.Context; |
07 | import android.view.View; |
08 | import android.view.ViewGroup; |
09 | import android.widget.BaseAdapter; |
10 | import android.widget.TextView; |
12 | * @title 圆角listview的实现 适配器 |
13 | * @description ListViewAdapter listview的适配器 |
14 | * @company 探索者网络工作室(www.tsz.net) |
15 | * <a href="http://my.oschina.net/arthor" class="referer" target="_blank">@author</a> michael Young (www.YangFuhai.com) |
19 | public class ListViewAdapter extends BaseAdapter { |
21 | private List<String> datas = new ArrayList<String>(); |
22 | private Context mContext; |
23 | public ListViewAdapter(Context c) { |
26 | public void addData(String strData){ |
27 | if (strData!= null ) datas.add(strData); |
30 | public void delData(){ |
31 | if (datas.size() > 0 ) datas.remove(datas.size() - 1 ); |
35 | public int getCount() { |
40 | public Object getItem( int arg0) { |
41 | return datas.get(arg0); |
45 | public long getItemId( int position) { |
50 | * listview中要判断item的位置,第一条,最后一条和中间的item是不一样的。 |
53 | public View getView( int position, View convertView, ViewGroup parent) { |
57 | view = View.inflate(mContext, R.layout.list_item_top, null ); |
58 | } else if (position == datas.size() - 1 ){ |
59 | view = View.inflate(mContext, R.layout.list_item_bottom, null ); |
61 | view = View.inflate(mContext, R.layout.list_item_middle, null ); |
64 | view = View.inflate(mContext, R.layout.list_item_single, null ); |
67 | ((TextView)view.findViewById(R.id.title)).setText(datas.get(position)); |
布局文件main.xml
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:layout_width = "wrap_content" |
04 | android:layout_height = "fill_parent" |
05 | android:orientation = "vertical" > |
08 | android:layout_width = "fill_parent" |
09 | android:layout_height = "wrap_content" |
10 | android:text = "请点击添加删除 查看效果" /> |
13 | android:layout_width = "match_parent" |
14 | android:layout_height = "wrap_content" |
15 | android:gravity = "center" > |
18 | android:id = "@+id/buttonAdd" |
19 | android:layout_width = "wrap_content" |
20 | android:layout_height = "wrap_content" |
25 | android:id = "@+id/buttonDel" |
26 | android:layout_width = "wrap_content" |
27 | android:layout_height = "wrap_content" |
33 | android:id = "@+id/listview" |
34 | android:layout_marginLeft = "10dip" |
35 | android:layout_marginRight = "10dip" |
36 | android:layout_width = "fill_parent" |
37 | android:layout_height = "wrap_content" /> |
listview的布局文件和资源文件
上面的代码很简单,没有什么可讲的。主要讲的是listview每个item的样式文件
listview的item有四个布局文件,分别是:只有一个item时候的样式,多个item时候上边item的样式,下边item的样式,中间item的样式。 布局文件和背景文件对应关系如下图所示:

文件文件
listview的item布局样式如下:
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | style = "@style/list_item_middle" |
04 | android:layout_width = "fill_parent" |
05 | android:layout_height = "wrap_content" |
06 | android:minHeight = "60dip" |
10 | android:id = "@+id/title" |
11 | style = "@style/content_page_large_text" |
12 | android:layout_width = "match_parent" |
13 | android:layout_height = "wrap_content" |
14 | android:text = "title" /> |
这四个布局文件中唯一不同的只是他们的stype属性(style=”@style/list_item_middle”)不同,也就是他们的背景不同。
资源文件
下面我们先贴出布局文件背景文件的shade代码,然后再仔细的讲解背景这些文件里面代码的意思。
background_view_rounded_bottom.xml
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < inset xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:insetBottom = "1.0px" |
04 | android:insetLeft = "1.0px" |
05 | android:insetRight = "1.0px" |
06 | android:insetTop = "1.0px" > |
09 | < item android:state_pressed = "true" > |
13 | android:endColor = "@color/base_end_color_pressed" |
14 | android:startColor = "@color/base_start_color_pressed" /> |
17 | android:bottomLeftRadius = "10.0dip" |
18 | android:bottomRightRadius = "10.0dip" |
19 | android:radius = "2.0dip" |
20 | android:topLeftRadius = "0.0dip" |
21 | android:topRightRadius = "0.0dip" /> |
28 | android:endColor = "@color/base_end_color_default" |
29 | android:startColor = "@color/base_start_color_default" /> |
32 | android:bottomLeftRadius = "11.0dip" |
33 | android:bottomRightRadius = "11.0dip" |
34 | android:radius = "2.0dip" |
35 | android:topLeftRadius = "0.0dip" |
36 | android:topRightRadius = "0.0dip" /> |
background_view_rounded_middle.xml:
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < inset xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:insetBottom = "0.0px" |
04 | android:insetLeft = "1.0px" |
05 | android:insetRight = "1.0px" |
06 | android:insetTop = "1.0px" > |
09 | < item android:state_pressed = "true" > |
13 | android:endColor = "@color/base_end_color_pressed" |
14 | android:startColor = "@color/base_start_color_pressed" /> |
16 | < corners android:radius = "0.0dip" /> |
23 | android:endColor = "@color/base_end_color_default" |
24 | android:startColor = "@color/base_start_color_default" /> |
26 | < corners android:radius = "0.0dip" /> |
background_view_rounded_single.xml :
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < inset xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:insetBottom = "1.0px" |
04 | android:insetLeft = "1.0px" |
05 | android:insetRight = "1.0px" |
06 | android:insetTop = "0.0px" > |
09 | < item android:state_pressed = "true" > |
13 | android:endColor = "@color/base_end_color_pressed" |
14 | android:startColor = "@color/base_start_color_pressed" /> |
16 | < corners android:radius = "11.0dip" /> |
23 | android:color = "@color/rounded_container_border" /> |
27 | android:endColor = "@color/base_end_color_default" |
28 | android:startColor = "@color/base_start_color_default" /> |
30 | < corners android:radius = "10.0dip" /> |
background_view_rounded_top.xml :
01 | <? xml version = "1.0" encoding = "UTF-8" ?> |
02 | < inset xmlns:android = "http://schemas.android.com/apk/res/android" |
03 | android:insetBottom = "0.0px" |
04 | android:insetLeft = "1.0px" |
05 | android:insetRight = "1.0px" |
06 | android:insetTop = "1.0px" > |
09 | < item android:state_pressed = "true" > |
13 | android:endColor = "@color/base_end_color_pressed" |
14 | android:startColor = "@color/base_start_color_pressed" /> |
17 | android:bottomLeftRadius = "0.0dip" |
18 | android:bottomRightRadius = "0.0dip" |
19 | android:radius = "2.0dip" |
20 | android:topLeftRadius = "10.0dip" |
21 | android:topRightRadius = "10.0dip" /> |
28 | android:endColor = "@color/base_end_color_default" |
29 | android:startColor = "@color/base_start_color_default" /> |
32 | android:bottomLeftRadius = "0.0dip" |
33 | android:bottomRightRadius = "0.0dip" |
34 | android:radius = "2.0dip" |
35 | android:topLeftRadius = "11.0dip" |
36 | android:topRightRadius = "11.0dip" /> |
listview资源文件讲解
我们拿 background_view_rounded_top.xml (最后一个资源文件)来给大家讲解 里面的每个属性的含义:
inset
inset:这种资源指向一个InsetDrawable对象,它能够用指定的距离嵌入到另一个可绘制资源中。它的属性有:
android:drawable=”@drawable/drawable_resource”
android:insetTop=”dimension”
android:insetRight=”dimension”
android:insetBottom=”dimension”
android:insetLeft=”dimension”
其中android:drawable是要嵌入的图片资源,android:insetXXX是嵌入位置。
selector
selector:是一种样式选择器,它用来指导某个view(button,textview,edittext等)的不同状态(比如:正常的状态,获得焦点的状态,按下的状态 等)对应的不同资源。
shade
shade gradient
shade gradient:颜色渐变
android:startColor和android:endColor分别为渐变的起始颜色和结束颜色
android:angle是渐变角度,必须为45的整数倍 。
渐变模式有两种:
android:type=”linear”,线性渐变,白话就是渐变是从一头到另一头的颜色渐变过程。
android:type=”radial”,径向渐变,白话就是渐变是从中心到四周的过程。径向渐变需要指定渐变半径android:gradientRadius=”50″。
shade corners
shade corners:圆角 (这篇文章主要就是用到了它)
android:radius为角的弧度,值越大角越圆。
其他
shade 除了gradient和corners以外,还有stroke(描边),solid (实心),padding等(具体大家可以看android的帮助文档)。
实现效果图
通过以上的代码,我们实现了效果如下图:



完毕
源码下载
源码下载:http://download.youkuaiyun.com/detail/michael_yy/4614701 (免积分下载)
转载请注明出处。http://blog.youkuaiyun.com/michael_yy/article/details/8038653 或 http://www.yangfuhai.com/topic/48.html (杨福海的博客)