黄色网页视频 I 影音先锋日日狠狠久久 I 秋霞午夜毛片 I 秋霞一二三区 I 国产成人片无码视频 I 国产 精品 自在自线 I av免费观看网站 I 日本精品久久久久中文字幕5 I 91看视频 I 看全色黄大色黄女片18 I 精品不卡一区 I 亚洲最新精品 I 欧美 激情 在线 I 人妻少妇精品久久 I 国产99视频精品免费专区 I 欧美影院 I 欧美精品在欧美一区二区少妇 I av大片网站 I 国产精品黄色片 I 888久久 I 狠狠干最新 I 看看黄色一级片 I 黄色精品久久 I 三级av在线 I 69色综合 I 国产日韩欧美91 I 亚洲精品偷拍 I 激情小说亚洲图片 I 久久国产视频精品 I 国产综合精品一区二区三区 I 色婷婷国产 I 最新成人av在线 I 国产私拍精品 I 日韩成人影音 I 日日夜夜天天综合

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

系統(tǒng) 2653 0

在項目中我們會經(jīng)常遇到這種圓角效果,因為直角的看起來確實不那么雅觀,可能大家會想到用圖片實現(xiàn),試想上中下要分別做三張圖片,這樣既會是自己的項目增大也會增加內(nèi)存使用量,所以使用shape來實現(xiàn)不失為一種更好的實現(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. <!--實心-->
  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分別為起始和結(jié)束顏色,ndroid:angle是漸變角度,必須為45的整數(shù)倍。
另外漸變默認的模式為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表示'-'這樣一個橫線的寬度,android:dashGap表示之間隔開的距離。

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


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

首先在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 >

如果是中間項,則應(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的圖片,因為9PNG圖片拉伸不失真。

定義好了圓角的shape接下來是自定義listview的實現(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. //只有一項
  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對應(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","反饋意見");
  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","支持我們,請點擊這里的廣告");
  47. listData.add(map);
  48. }
  49. }

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

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



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


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


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

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯(lián)系: 360901061

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

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發(fā)表我的評論
最新評論 總共0條評論