亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

android自定義listview實(shí)現(xiàn)圓角

系統(tǒng) 2513 0

在項(xiàng)目中我們會(huì)經(jīng)常遇到這種圓角效果,因?yàn)橹苯堑目雌饋?lái)確實(shí)不那么雅觀,可能大家會(huì)想到用圖片實(shí)現(xiàn),試想上中下要分別做三張圖片,這樣既會(huì)是自己的項(xiàng)目增大也會(huì)增加內(nèi)存使用量,所以使用shape來(lái)實(shí)現(xiàn)不失為一種更好的實(shí)現(xiàn)方式。在這里先看一下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. <!--實(shí)心-->
  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:實(shí)心,就是填充的意思
android:color指定填充的顏色

gradient:漸變

android:startColor和android:endColor分別為起始和結(jié)束顏色,ndroid:angle是漸變角度,必須為45的整數(shù)倍。
另外漸變默認(rèn)的模式為android:type="linear",即線性漸變,可以指定漸變?yōu)閺较驖u變,android:type="radial",徑向漸變需要指定半徑android:gradientRadius="50"。

stroke:描邊
android:width="2dp" 描邊的寬度,android:color 描邊的顏色。

我們還可以把描邊弄成虛線的形式,設(shè)置方式為:
android:dashWidth="5dp"
android:dashGap="3dp"
其中android:dashWidth表示'-'這樣一個(gè)橫線的寬度,android:dashGap表示之間隔開(kāi)的距離。

corners:圓角
android:radius為角的弧度,值越大角越圓。


OK,下面開(kāi)始自定義listview實(shí)現(xiàn)圓角的例子:

首先在drawable下定義只有一項(xiàng)的選擇器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. <!--實(shí)心-->
  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 >
如果是頂部第一項(xiàng),則上面兩個(gè)角為圓角,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 >

如果是底部最后一項(xiàng),則下面兩個(gè)角為圓角,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 >

如果是中間項(xiàng),則應(yīng)該不需要圓角, 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的圖片,因?yàn)?PNG圖片拉伸不失真。

定義好了圓角的shape接下來(lái)是自定義listview的實(shí)現(xiàn):

  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. //只有一項(xiàng)
  34. setSelector(R.drawable.app_list_corner_round);
  35. }else{
  36. //第一項(xiàng)
  37. setSelector(R.drawable.app_list_corner_round_top);
  38. }
  39. }elseif( itemnum ==(getAdapter().getCount()-1))
  40. //最后一項(xiàng)
  41. setSelector(R.drawable.app_list_corner_round_bottom);
  42. else{
  43. //中間項(xiàng)
  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對(duì)應(yīng)的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("設(shè)置");
  27. }
  28. /**
  29. *設(shè)置列表數(shù)據(jù)
  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","反饋意見(jiàn)");
  41. listData.add(map);
  42. map = new HashMap < String ,String > ();
  43. map.put("text","關(guān)于我們");
  44. listData.add(map);
  45. map = new HashMap < String ,String > ();
  46. map.put("text","支持我們,請(qǐng)點(diǎn)擊這里的廣告");
  47. listData.add(map);
  48. }
  49. }

這樣就完成了,雖然過(guò)程較繁雜,但是當(dāng)做一個(gè)好的模板以后使用會(huì)方便很多,最后看一下實(shí)現(xiàn)效果和我們用圖片實(shí)現(xiàn)的一樣嗎

android自定義listview實(shí)現(xiàn)圓角



android自定義listview實(shí)現(xiàn)圓角


android自定義listview實(shí)現(xiàn)圓角


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點(diǎn)擊下面給點(diǎn)支持吧,站長(zhǎng)非常感激您!手機(jī)微信長(zhǎng)按不能支付解決辦法:請(qǐng)將微信支付二維碼保存到相冊(cè),切換到微信,然后點(diǎn)擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對(duì)您有幫助就好】

您的支持是博主寫(xiě)作最大的動(dòng)力,如果您喜歡我的文章,感覺(jué)我的文章對(duì)您有幫助,請(qǐng)用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長(zhǎng)會(huì)非常 感謝您的哦!!!

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 天堂素人在线 | 久久成人亚洲 | 欧美乱大交xxxxxx喷潮免费 | 色婷婷综合欧美成人 | 四虎影视在线观看2413 | 在线观看亚洲成人 | 亚欧在线视频 | 精品中文字幕在线 | 精品在线小视频 | 亚洲综合色网 | 国产欧美日韩图片一区二区 | 一级做a爰片性色毛片2021 | 免看一级一片一在线看 | 久久小视频 | 日本大片免a费观看在线 | 欧美亚洲精品一区二三 | 午夜精品久久久久久久 | 亚洲一区免费在线观看 | 久久99精品一久久久久久 | 福利毛片 | 免费一级大毛片a一观看不卡 | 男女一级毛片免费播放 | 91麻豆精品国产91久久久久久 | 奇米影视亚洲春色77777 | 草逼网站 | 波多野结衣绝顶大高潮 | 天天操免费视频 | 国产福利视屏 | 一区二区三区在线免费视频 | 成人欧美一区二区三区在线 | 老司机一级毛片 | 亚洲欧美国产五月天综合 | 久久久精品久久久久三级 | 国产精品66在线观看 | 四虎精品影视 | 欧美黑人巨大肥婆性视频 | 日本免费人做人一区在线观看 | 黄色网址视频在线播放 | 色综合狠狠操 | 国产亚洲综合成人91精品 | 99精品久久久久久 |