欧美三区_成人在线免费观看视频_欧美极品少妇xxxxⅹ免费视频_a级毛片免费播放_鲁一鲁中文字幕久久_亚洲一级特黄

android自定義listview實現圓角

系統 2284 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條評論
主站蜘蛛池模板: 天天做天天爽 | 欧美日韩国产一区二区三区不卡 | 99热播放 | 九九色综合 | 亚洲美女网站 | 欧美日韩在线第一页 | 欧美日韩一区二区三区在线观看 | 站长推荐国产午夜免费视频 | 亚洲欧美偷拍自拍 | 一道本视频在线观看 | 国产精品视频久久久 | 色偷偷成人网免费视频男人的天堂 | 久草新视频| 亚洲一区国产视频 | 婷婷六月综合网 | 日韩精品在线视频 | 亚洲国产精品成 | 日韩精品久久久久久 | 国产短视频精品区第一页 | 成人免费一区二区三区视频软件 | 亚洲午夜大片 | 欧美三级一区 | 日本三级2020 | 日韩av不卡在线 | 精品国产一区二区在线 | 日韩性色| 午夜电影免费看 | 一区免费看 | 五月中文字幕 | 玖玖精品 | 九九99热久久精品在线9 | 我要看免费毛片 | 国产精品不卡一区 | 一区二区三区成人A片在线观看 | 亚洲精品日韩在线 | 欧美亚洲 尤物久久 综合精品 | 欧美午夜视频一区二区三区 | 婷婷丁香色综合图亚洲 | 99免费| 啪啪小视频网站 | 欧美精品人爱a欧美精品 |