android自定义listview实现圆角

系统 1974 0

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

  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < shape xmlns: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. <!-- < stroke android:width = "1dip"
  13. android:color = "@color/blue"
  14. /> -- >
  15. <!--实心-->
  16. <!-- < solid android: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:

  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < shape xmlns: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. <!-- < stroke android:width = "1dip"
  13. android:color = "@color/blue"
  14. /> -- >
  15. <!--实心-->
  16. <!-- < solid android: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:

  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < shape xmlns: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. <!-- < stroke android:width = "1dip"
  11. android:color = "@color/blue"
  12. /> -- >
  13. <!-- < solid android:color = "#FFeaeaea"
  14. /> -- >
  15. < corners
  16. android:topLeftRadius = "4dip"
  17. android:topRightRadius = "4dip"
  18. />
  19. </ shape >

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

  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < shape xmlns: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. <!-- < stroke android:width = "1dip"
  11. android:color = "@color/blue"
  12. /> -- >
  13. <!-- < solid android:color = "#FFeaeaea"
  14. /> -- >
  15. < corners
  16. android:bottomRightRadius = "4dip"
  17. android:bottomLeftRadius = "4dip"
  18. />
  19. </ shape >

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

  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < shape xmlns: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. <!-- < stroke android:width = "1dip"
  11. android:color = "@color/blue"
  12. /> -- >
  13. <!-- < solid android:color = "#FFeaeaea"
  14. /> -- >
  15. <!-- < corners
  16. android:bottomRightRadius = "4dip"
  17. android:bottomLeftRadius = "4dip"
  18. /> -- >
  19. </ shape >

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

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

  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. int x =(int)ev.getX();
  26. int y =(int)ev.getY();
  27. int itemnum = 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:

  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < LinearLayout xmlns: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. < include layout = "@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

  1. <? xml version = "1.0" encoding = "utf-8" ?>
  2. < LinearLayout xmlns: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中填充适配器:

  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. privateCornerListView cornerListView = null ;
  12. privateList < Map < String ,String > > listData = null ;
  13. privateSimpleAdapter adapter = 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 = new SimpleAdapter(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 = new ArrayList < Map < String ,String > > ();
  33. Map < String ,String > map = new HashMap < String ,String > ();
  34. map.put("text","分享");
  35. listData.add(map);
  36. map = new HashMap < String ,String > ();
  37. map.put("text","检查新版本");
  38. listData.add(map);
  39. map = new HashMap < String ,String > ();
  40. map.put("text","反馈意见");
  41. listData.add(map);
  42. map = new HashMap < String ,String > ();
  43. map.put("text","关于我们");
  44. listData.add(map);
  45. map = new HashMap < String ,String > ();
  46. map.put("text","支持我们,请点击这里的广告");
  47. listData.add(map);
  48. }
  49. }

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

android自定义listview实现圆角



android自定义listview实现圆角


android自定义listview实现圆角


更多文章、技术交流、商务合作、联系博主

微信扫码或搜索:z360901061

微信扫一扫加我为好友

QQ号联系: 360901061

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描下面二维码支持博主2元、5元、10元、20元等您想捐的金额吧,狠狠点击下面给点支持吧,站长非常感激您!手机微信长按不能支付解决办法:请将微信支付二维码保存到相册,切换到微信,然后点击微信右上角扫一扫功能,选择支付二维码完成支付。

【本文对您有帮助就好】

您的支持是博主写作最大的动力,如果您喜欢我的文章,感觉我的文章对您有帮助,请用微信扫描上面二维码支持博主2元、5元、10元、自定义金额等您想捐的金额吧,站长会非常 感谢您的哦!!!

发表我的评论
最新评论 总共0条评论