你好,游客 登录 注册 搜索
背景:
阅读新闻

Android自定义ListView实现圆角

[日期:2012-07-25] 来源:csdn  作者:wangkuifeng0118 [字体:大 中 小]

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

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

  1. package cn.com.karl.view;  
  2.   
  3. import cn.com.karl.test.R;  
  4. import Android.content.Context;  
  5. import android.util.AttributeSet;  
  6. import android.view.MotionEvent;  
  7. import android.widget.AdapterView;  
  8. import android.widget.ListView;  
  9.   
  10. /**  
  11.  * 圆角ListView  
  12.  */  
  13. public class CornerListView extends ListView {  
  14.     public CornerListView(Context context) {  
  15.         super(context);  
  16.     }  
  17.   
  18.     public CornerListView(Context context, AttributeSet attrs, int defStyle) {  
  19.         super(context, attrs, defStyle);  
  20.     }  
  21.   
  22.     public CornerListView(Context context, AttributeSet attrs) {  
  23.         super(context, attrs);  
  24.     }  
  25.   
  26.     @Override  
  27.     public boolean onInterceptTouchEvent(MotionEvent ev) {  
  28.         switch (ev.getAction()) {  
  29.         case MotionEvent.ACTION_DOWN:  
  30.             int x = (int) ev.getX();  
  31.             int y = (int) ev.getY();  
  32.             int itemnum = pointToPosition(x, y);  
  33.             if (itemnum == AdapterView.INVALID_POSITION)  
  34.                 break;  
  35.             else {  
  36.                 if (itemnum == 0) {  
  37.                     if (itemnum == (getAdapter().getCount() - 1)) {  
  38.                         //只有一项  
  39.                         setSelector(R.drawable.app_list_corner_round);  
  40.                     } else {  
  41.                         //第一项  
  42.                         setSelector(R.drawable.app_list_corner_round_top);  
  43.                     }  
  44.                 } else if (itemnum == (getAdapter().getCount() - 1))  
  45.                     //最后一项  
  46.                     setSelector(R.drawable.app_list_corner_round_bottom);  
  47.                 else {  
  48.                     //中间项  
  49.                     setSelector(R.drawable.app_list_corner_round_center);  
  50.                 }  
  51.             }  
  52.             break;  
  53.         case MotionEvent.ACTION_UP:  
  54.             break;  
  55.         }  
  56.         return super.onInterceptTouchEvent(ev);  
  57.     }  
  58. }  

下面看一下列表布局文件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.   
  7.     <include layout="@layout/head" />  
  8.   
  9.     <cn.com.karl.view.CornerListView  
  10.         android:id="@+id/setting_list"  
  11.         android:layout_width="fill_parent"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_margin="10dip"  
  14.         android:background="@drawable/corner_list_bg"  
  15.         android:cacheColorHint="#00000000" />  
  16.   
  17. </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.         
  6.         <RelativeLayout    
  7.             android:layout_width="fill_parent"    
  8.             android:layout_height="50dp"     
  9.             android:gravity="center_horizontal">    
  10.         
  11.             <TextView    
  12.                 android:id="@+id/tv_system_title"    
  13.                 android:layout_width="wrap_content"    
  14.                 android:layout_height="wrap_content"    
  15.                 android:layout_alignParentLeft="true"    
  16.                 android:layout_centerVertical="true"    
  17.                 android:layout_marginLeft="10dp"    
  18.                 android:text="分享"     
  19.                 android:textColor="@color/black"/>    
  20.         
  21.             <ImageView    
  22.                 android:id="@+id/iv_system_right"    
  23.                 android:layout_width="wrap_content"    
  24.                 android:layout_height="wrap_content"    
  25.                 android:layout_alignParentRight="true"    
  26.                 android:layout_centerVertical="true"    
  27.                 android:layout_marginRight="10dp"    
  28.                 android:src="@drawable/arrow1" />    
  29.         </RelativeLayout>    
  30.         
  31.     </LinearLayout>    
linux
相关资讯       Android开发入门 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款