Android实现圆角ListView效果
在项目开发中我们可能会碰到圆角listview效果,因为直角的看起来确实不那么雅观,可能大家会想到用图片实现,试想上中下要分别做三张图片,这样做太繁琐,这时使用shape来实现不失为一种更好的实现方式。
先看一下android 中shape的使用方法:
solid:实心,就是填充的意思
android:color指定填充的颜色
gradient:渐变
android:startcolor和android:endcolor分别为起始和结束颜色,ndroid:angle是渐变角度,必须为45的整数倍。
另外渐变默认的模式为android:type="linear",即线性渐变,可以指定渐变为径向渐变,android:type="radial",径向渐变需要指定半径android:gradientradius="50"。
stroke:描边
android:width="2dp" 描边的宽度,android:color 描边的颜色。
我们还可以把描边弄成虚线的形式,设置方式为:
android:dashwidth="5dp"
android:dashgap="3dp"
其中android:dashwidth表示'-'这样一个横线的宽度,android:dashgap表示之间隔开的距离。
corners:圆角
android:radius为角的弧度,值越大角越圆。
当然,这里并不是说这种圆角的列表一段是listview来实现的,可能是由多个linearlayout/relativelayout叠起来的。这个就看你怎么取舍了;如果列表项固定不怎么变化可以采取后者来实现比较好,如果需要动态变化那么使用listview来实现更优。
下面来定义一下listview只有一项时的背景(上下两个角都是圆角) app_list_corner_round.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <!-- 渐变 --> <gradient android:angle="270" android:endcolor="@color/white" android:startcolor="@color/white" /> <!-- 圆角 --> <corners android:bottomleftradius="4dip" android:bottomrightradius="4dip" android:topleftradius="4dip" android:toprightradius="4dip" /> </shape>
listview第一项的背景(上面是圆角,下面是直角) app_list_corner_round_top.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:angle="270" android:endcolor="@color/white" android:startcolor="@color/white" /> <corners android:topleftradius="@dimen/app_list_radius" android:toprightradius="@dimen/app_list_radius" /> </shape>
listview最后一项的背景(上面是直角,下面是圆角) app_list_corner_round_bottom.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:angle="270" android:endcolor="@color/white" android:startcolor="@color/white" /> <corners android:bottomleftradius="@dimen/app_list_radius" android:bottomrightradius="@dimen/app_list_radius" /> </shape>
listview中间项的背景(上下都是直角) app_list_corner_round_center.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <gradient android:angle="270" android:endcolor="@color/white" android:startcolor="@color/white" /> </shape>
接下来先看看adapter的实现
package com.example.roundcorner.adapter; import java.util.list; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.imageview; import android.widget.textview; import com.example.roundcorner.r; import com.example.roundcorner.entity.listbean; public class listadapter extends baseadapter { private list<listbean> mlist; private context mcontext; public listadapter(context mcontext,list<listbean> mlist) { this.mlist = mlist; this.mcontext = mcontext.getapplicationcontext(); } @override public int getcount() { return this.mlist.size(); } @override public object getitem(int position) { return this.mlist.get(position); } @override public long getitemid(int position) { return position; } @override public int getitemviewtype(int position) { // todo auto-generated method stub return super.getitemviewtype(position); } @override public int getviewtypecount() { // todo auto-generated method stub return super.getviewtypecount(); } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; if (convertview == null) { holder = new viewholder(); convertview = layoutinflater.from(this.mcontext).inflate( r.layout.listview_item, null, false); holder.textview = (textview) convertview .findviewbyid(r.id.listview_item_textview); holder.imageview = (imageview) convertview .findviewbyid(r.id.listview_item_imageview); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } if(position==0){ if(position == getcount()-1){ //只有一项 convertview.setbackgroundresource(r.drawable.app_list_corner_round); }else{ //第一项 convertview.setbackgroundresource(r.drawable.app_list_corner_round_top); } }else if(position == getcount()-1){ convertview.setbackgroundresource(r.drawable.app_list_corner_round_bottom); }else{ convertview.setbackgroundresource(r.drawable.app_list_corner_round_center); } listbean lb = mlist.get(position); holder.textview.settext(lb.getkey()); return convertview; } static class viewholder { textview textview; imageview imageview; } }
listview_item.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content"> <textview android:id="@+id/listview_item_textview" android:layout_width="wrap_content" android:layout_height="48dp" android:paddingleft="10dp" android:gravity="center_vertical" android:layout_centervertical="true" android:text="a-h" android:textcolor="@color/black" android:textsize="20sp" /> <imageview android:id="@+id/listview_item_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/arrow" android:layout_alignparentright="true" android:layout_centervertical="true" /> </relativelayout>
最后看看主界面activity的实现
package com.example.roundcorner; import java.util.arraylist; import java.util.list; import android.app.activity; import android.os.bundle; import android.widget.listview; import com.example.roundcorner.adapter.listadapter; import com.example.roundcorner.entity.listbean; public class mainactivity extends activity { private list<listbean> data; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); initdata(); findview(); } private void findview() { listview mlistview = (listview) findviewbyid(r.id.mlistview); listadapter madapter = new listadapter(this,data); mlistview.setadapter(madapter); } private void initdata() { data = new arraylist<listbean>(); for (int i = 0; i < 5; i++) { listbean lb = new listbean(); lb.setkey("设置 "+i); data.add(lb); } } }
activity_main.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".mainactivity" > <textview android:layout_width="match_parent" android:layout_height="48dp" android:background="@color/white" android:gravity="center" android:text="设置" android:textsize="20sp" /> <relativelayout android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" > <listview android:id="@+id/mlistview" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@drawable/app_list_round" android:cachecolorhint="@android:color/transparent" android:divider="@drawable/app_list_divider" android:dividerheight="2dip" android:padding="2dp" /> </relativelayout> </linearlayout>
最后看看实现的效果
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。