android自定义listview实现圆角

本文介绍如何使用Shape XML实现界面元素的圆角效果,包括渐变、描边等特性,并通过自定义ListView实现列表项不同位置的圆角样式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

在项目中我们会经常遇到这种圆角效果,因为直角的看起来确实不那么雅观,可能大家会想到用图片实现,试想上中下要分别做三张图片,这样既会是自己的项目增大也会增加内存使用量,所以使用shape来实现不失为一种更好的实现方式。在这里先看一下shape的使用:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <shapexmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle"
  4. >
  5. <!--渐变-->
  6. <gradient
  7. android:startColor="#B5E7B8"
  8. android:endColor="#76D37B"
  9. android:angle="270"
  10. />
  11. <!--描边-->
  12. <!--<strokeandroid:width="1dip"
  13. android:color="@color/blue"
  14. />-->
  15. <!--实心-->
  16. <!--<solidandroid:color="#FFeaeaea"
  17. />-->
  18. <!--圆角-->
  19. <corners
  20. android:bottomRightRadius="4dip"
  21. android:bottomLeftRadius="4dip"
  22. android:topLeftRadius="4dip"
  23. android:topRightRadius="4dip"
  24. />
  25. </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为角的弧度,值越大角越圆。


OK,下面开始自定义listview实现圆角的例子:

首先在drawable下定义只有一项的选择器app_list_corner_round.xml:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <shapexmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle"
  4. >
  5. <!--渐变-->
  6. <gradient
  7. android:startColor="#B5E7B8"
  8. android:endColor="#76D37B"
  9. android:angle="270"
  10. />
  11. <!--描边-->
  12. <!--<strokeandroid:width="1dip"
  13. android:color="@color/blue"
  14. />-->
  15. <!--实心-->
  16. <!--<solidandroid:color="#FFeaeaea"
  17. />-->
  18. <!--圆角-->
  19. <corners
  20. android:bottomRightRadius="4dip"
  21. android:bottomLeftRadius="4dip"
  22. android:topLeftRadius="4dip"
  23. android:topRightRadius="4dip"
  24. />
  25. </shape>
如果是顶部第一项,则上面两个角为圆角,app_list_corner_round_top.xml:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <shapexmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle"
  4. >
  5. <gradient
  6. android:startColor="#B5E7B8"
  7. android:endColor="#76D37B"
  8. android:angle="270"
  9. />
  10. <!--<strokeandroid:width="1dip"
  11. android:color="@color/blue"
  12. />-->
  13. <!--<solidandroid:color="#FFeaeaea"
  14. />-->
  15. <corners
  16. android:topLeftRadius="4dip"
  17. android:topRightRadius="4dip"
  18. />
  19. </shape>

如果是底部最后一项,则下面两个角为圆角,app_list_corner_round_bottom.xml:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <shapexmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle"
  4. >
  5. <gradient
  6. android:startColor="#B5E7B8"
  7. android:endColor="#76D37B"
  8. android:angle="270"
  9. />
  10. <!--<strokeandroid:width="1dip"
  11. android:color="@color/blue"
  12. />-->
  13. <!--<solidandroid:color="#FFeaeaea"
  14. />-->
  15. <corners
  16. android:bottomRightRadius="4dip"
  17. android:bottomLeftRadius="4dip"
  18. />
  19. </shape>

如果是中间项,则应该不需要圆角, app_list_corner_round_center.xml:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <shapexmlns:android="http://schemas.android.com/apk/res/android"
  3. android:shape="rectangle"
  4. >
  5. <gradient
  6. android:startColor="#B5E7B8"
  7. android:endColor="#76D37B"
  8. android:angle="270"
  9. />
  10. <!--<strokeandroid:width="1dip"
  11. android:color="@color/blue"
  12. />-->
  13. <!--<solidandroid:color="#FFeaeaea"
  14. />-->
  15. <!--<corners
  16. android:bottomRightRadius="4dip"
  17. android:bottomLeftRadius="4dip"
  18. />-->
  19. </shape>

listview的背景图片大家可以使用stroke描述,这里我使用了一张9PNG的图片,因为9PNG图片拉伸不失真。

定义好了圆角的shape接下来是自定义listview的实现:

[html] view plain copy
  1. packagecn.com.karl.view;
  2. importcn.com.karl.test.R;
  3. importandroid.content.Context;
  4. importandroid.util.AttributeSet;
  5. importandroid.view.MotionEvent;
  6. importandroid.widget.AdapterView;
  7. importandroid.widget.ListView;
  8. /**
  9. *圆角ListView
  10. */
  11. publicclassCornerListViewextendsListView{
  12. publicCornerListView(Contextcontext){
  13. super(context);
  14. }
  15. publicCornerListView(Contextcontext,AttributeSetattrs,intdefStyle){
  16. super(context,attrs,defStyle);
  17. }
  18. publicCornerListView(Contextcontext,AttributeSetattrs){
  19. super(context,attrs);
  20. }
  21. @Override
  22. publicbooleanonInterceptTouchEvent(MotionEventev){
  23. switch(ev.getAction()){
  24. caseMotionEvent.ACTION_DOWN:
  25. intx=(int)ev.getX();
  26. inty=(int)ev.getY();
  27. intitemnum=pointToPosition(x,y);
  28. if(itemnum==AdapterView.INVALID_POSITION)
  29. break;
  30. else{
  31. if(itemnum==0){
  32. if(itemnum==(getAdapter().getCount()-1)){
  33. //只有一项
  34. setSelector(R.drawable.app_list_corner_round);
  35. }else{
  36. //第一项
  37. setSelector(R.drawable.app_list_corner_round_top);
  38. }
  39. }elseif(itemnum==(getAdapter().getCount()-1))
  40. //最后一项
  41. setSelector(R.drawable.app_list_corner_round_bottom);
  42. else{
  43. //中间项
  44. setSelector(R.drawable.app_list_corner_round_center);
  45. }
  46. }
  47. break;
  48. caseMotionEvent.ACTION_UP:
  49. break;
  50. }
  51. returnsuper.onInterceptTouchEvent(ev);
  52. }
  53. }

下面看一下列表布局文件setting。xml:

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical">
  6. <includelayout="@layout/head"/>
  7. <cn.com.karl.view.CornerListView
  8. android:id="@+id/setting_list"
  9. android:layout_width="fill_parent"
  10. android:layout_height="wrap_content"
  11. android:layout_margin="10dip"
  12. android:background="@drawable/corner_list_bg"
  13. android:cacheColorHint="#00000000"/>
  14. </LinearLayout>

自定义Listview对应的item文件 main_tab_setting_list_item.xml

[html] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="wrap_content">
  5. <RelativeLayout
  6. android:layout_width="fill_parent"
  7. android:layout_height="50dp"
  8. android:gravity="center_horizontal">
  9. <TextView
  10. android:id="@+id/tv_system_title"
  11. android:layout_width="wrap_content"
  12. android:layout_height="wrap_content"
  13. android:layout_alignParentLeft="true"
  14. android:layout_centerVertical="true"
  15. android:layout_marginLeft="10dp"
  16. android:text="分享"
  17. android:textColor="@color/black"/>
  18. <ImageView
  19. android:id="@+id/iv_system_right"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:layout_alignParentRight="true"
  23. android:layout_centerVertical="true"
  24. android:layout_marginRight="10dp"
  25. android:src="@drawable/arrow1"/>
  26. </RelativeLayout>
  27. </LinearLayout>

最后是在activity中填充适配器:

[html] view plain copy
  1. packagecn.com.karl.test;
  2. importjava.util.ArrayList;
  3. importjava.util.HashMap;
  4. importjava.util.List;
  5. importjava.util.Map;
  6. importcn.com.karl.view.CornerListView;
  7. importandroid.os.Bundle;
  8. importandroid.view.View;
  9. importandroid.widget.SimpleAdapter;
  10. publicclassSettingActivityextendsBaseActivity{
  11. privateCornerListViewcornerListView=null;
  12. privateList<Map<String,String>>listData=null;
  13. privateSimpleAdapteradapter=null;
  14. @Override
  15. protectedvoidonCreate(BundlesavedInstanceState){
  16. //TODOAuto-generatedmethodstub
  17. super.onCreate(savedInstanceState);
  18. setContentView(R.layout.setting);
  19. cornerListView=(CornerListView)findViewById(R.id.setting_list);
  20. setListData();
  21. adapter=newSimpleAdapter(getApplicationContext(),listData,R.layout.main_tab_setting_list_item,
  22. newString[]{"text"},newint[]{R.id.tv_system_title});
  23. cornerListView.setAdapter(adapter);
  24. initHead();
  25. btn_leftTop.setVisibility(View.INVISIBLE);
  26. tv_head.setText("设置");
  27. }
  28. /**
  29. *设置列表数据
  30. */
  31. privatevoidsetListData(){
  32. listData=newArrayList<Map<String,String>>();
  33. Map<String,String>map=newHashMap<String,String>();
  34. map.put("text","分享");
  35. listData.add(map);
  36. map=newHashMap<String,String>();
  37. map.put("text","检查新版本");
  38. listData.add(map);
  39. map=newHashMap<String,String>();
  40. map.put("text","反馈意见");
  41. listData.add(map);
  42. map=newHashMap<String,String>();
  43. map.put("text","关于我们");
  44. listData.add(map);
  45. map=newHashMap<String,String>();
  46. map.put("text","支持我们,请点击这里的广告");
  47. listData.add(map);
  48. }
  49. }

这样就完成了,虽然过程较繁杂,但是当做一个好的模板以后使用会方便很多,最后看一下实现效果和我们用图片实现的一样吗





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值