listview的背景图片大家可以使用stroke描述,这里我使用了一张9PNG的图片,因为9PNG图片拉伸不失真。
定义好了圆角的shape接下来是自定义listview的实现:
- package cn.com.karl.view;
- import cn.com.karl.test.R;
- import Android.content.Context;
- import android.util.AttributeSet;
- import android.view.MotionEvent;
- import android.widget.AdapterView;
- import android.widget.ListView;
- /**
- * 圆角ListView
- */
- public class CornerListView extends ListView {
- public CornerListView(Context context) {
- super(context);
- }
- public CornerListView(Context context, AttributeSet attrs, int defStyle) {
- super(context, attrs, defStyle);
- }
- public CornerListView(Context context, AttributeSet attrs) {
- super(context, attrs);
- }
- @Override
- public boolean onInterceptTouchEvent(MotionEvent ev) {
- switch (ev.getAction()) {
- case MotionEvent.ACTION_DOWN:
- int x = (int) ev.getX();
- int y = (int) ev.getY();
- int itemnum = pointToPosition(x, y);
- if (itemnum == AdapterView.INVALID_POSITION)
- break;
- else {
- if (itemnum == 0) {
- if (itemnum == (getAdapter().getCount() - 1)) {
- //只有一项
- setSelector(R.drawable.app_list_corner_round);
- } else {
- //第一项
- setSelector(R.drawable.app_list_corner_round_top);
- }
- } else if (itemnum == (getAdapter().getCount() - 1))
- //最后一项
- setSelector(R.drawable.app_list_corner_round_bottom);
- else {
- //中间项
- setSelector(R.drawable.app_list_corner_round_center);
- }
- }
- break;
- case MotionEvent.ACTION_UP:
- break;
- }
- return super.onInterceptTouchEvent(ev);
- }
- }
下面看一下列表布局文件setting。xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:orientation="vertical" >
- <include layout="@layout/head" />
- <cn.com.karl.view.CornerListView
- android:id="@+id/setting_list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_margin="10dip"
- android:background="@drawable/corner_list_bg"
- android:cacheColorHint="#00000000" />
- </LinearLayout>
自定义Listview对应的item文件 main_tab_setting_list_item.xml
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- <RelativeLayout
- android:layout_width="fill_parent"
- android:layout_height="50dp"
- android:gravity="center_horizontal">
- <TextView
- android:id="@+id/tv_system_title"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_centerVertical="true"
- android:layout_marginLeft="10dp"
- android:text="分享"
- android:textColor="@color/black"/>
- <ImageView
- android:id="@+id/iv_system_right"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_centerVertical="true"
- android:layout_marginRight="10dp"
- android:src="@drawable/arrow1" />
- </RelativeLayout>
- </LinearLayout>
