Android ListView圆角列表完整(附源码)

自己很少写文章,今天突然想做个记录,也是给自己做个笔记.

为应用项目需求,需要做图下效果,找了一下资料,自己在重新写一下,变Ok了,看效果图片再上代码:



主布局文件more_settings.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/setting_container"
    style="@style/Global_Bg"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <LinearLayout
        android:id="@+id/_top_box"
        android:layout_width="fill_parent"
        android:layout_height="@dimen/top_box"
        android:layout_alignParentTop="true" >

        <include
            android:id="@+id/inc_top_box"
            layout="@layout/top_box" />
    </LinearLayout>

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:scrollbars="none"
        android:layout_below="@+id/_top_box" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:orientation="vertical">
			
            <TextView 
                android:id="@+id/tv_state"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:layout_marginLeft="15dp"
                android:text="状态设置"
                android:textColor="@color/black"/>
            <cn.fengwoo.core.CornerListView 
                android:id="@+id/lv_state"
                android:layout_width="fill_parent"
		        android:layout_height="200dp"
		        android:layout_marginTop="5dp"
		        android:layout_marginLeft="15dp"
		        android:layout_marginRight="15dp"
		        android:background="@drawable/shape_bg_listview"
		        android:scrollbars="none"
		        android:cacheColorHint="@null"/>
                
            <TextView 
                android:id="@+id/tv_account"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:layout_marginLeft="15dp"
                android:text="帐号管理"
                android:textColor="@color/black"/>
            <cn.fengwoo.core.CornerListView 
                android:id="@+id/lv_account"
                android:layout_width="fill_parent"
		        android:layout_height="100dp"
		        android:layout_marginTop="5dp"
		        android:layout_marginLeft="15dp"
		        android:layout_marginRight="15dp"
		        android:background="@drawable/shape_bg_listview"
		        android:scrollbars="none"
		        android:cacheColorHint="@null"/>
                
            <TextView 
                android:id="@+id/tv_system"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="10dip"
                android:layout_marginLeft="15dp"
                android:text="系统帮助"
                android:textColor="@color/black"/>
            <cn.fengwoo.core.CornerListView 
                android:id="@+id/lv_system"
                android:layout_width="fill_parent"
		        android:layout_height="350dp"
		        android:layout_marginTop="5dp"
		        android:layout_marginLeft="15dp"
		        android:layout_marginRight="15dp"
		        android:background="@drawable/shape_bg_listview"
		        android:scrollbars="none"
		        android:cacheColorHint="@null"/>
                
            <cn.fengwoo.core.CornerListView 
                android:id="@+id/lv_exit"
                android:layout_width="fill_parent"
		        android:layout_height="100dp"
		        android:layout_marginTop="10dp"
		        android:layout_marginLeft="15dp"
		        android:layout_marginRight="15dp"
		        android:layout_marginBottom="20dp"
		        android:background="@drawable/shape_bg_listview"
		        android:scrollbars="none"
		        android:cacheColorHint="@null"/>
                

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

副布局文件(也就上面四个listview的item)

state_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp" 
        android:gravity="center_horizontal">

        <TextView
            android:id="@+id/tv_state_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"/>

        <TextView
            android:id="@+id/tv_state_context"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toLeftOf="@+id/iv_state_right"
            android:layout_centerVertical="true"
            android:layout_marginRight="10dp"
            android:text="对所有人可见" />

        <ImageView
            android:id="@+id/iv_state_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/arrows_right" />
    </RelativeLayout>

</LinearLayout>

account_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp" 
        android:gravity="center_horizontal">

        <TextView
            android:id="@+id/tv_account_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_account_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/arrows_right" />
    </RelativeLayout>

</LinearLayout>

system_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <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/arrows_right" />
    </RelativeLayout>

</LinearLayout>

exit_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="50dp" 
        android:gravity="center_horizontal">

        <TextView
            android:id="@+id/tv_exit_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/red"
            android:textStyle="bold"/>

        <ImageView
            android:id="@+id/iv_exit_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/arrows_right" />
    </RelativeLayout>

</LinearLayout>

布局基本就这些,下面看绘制圆角布局文件,总共有四个

上面左右圆角

app_list_corner_round_top.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#BFEEFF" 
        android:endColor="#40B9FF" 
        android:angle="270"/>
    <corners android:topLeftRadius="6dip"
        android:topRightRadius="6dip"/>
</shape> 

中部平角

app_list_corner_round.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#BFEEFF" 
        android:endColor="#40B9FF" 
        android:angle="270"/>
    <corners android:topLeftRadius="6dip"
        android:topRightRadius="6dip"
        android:bottomLeftRadius="6dip"
        android:bottomRightRadius="6dip"/>
</shape> 

下面左右圆角

app_list_corner_round_bottom.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
    <gradient android:startColor="#BFEEFF" 
        android:endColor="#40B9FF" 
        android:angle="270"/>
    <corners android:bottomLeftRadius="6dip"
        android:bottomRightRadius="6dip" />
</shape> 

上面是控制listview中item效果

下面是要改变listview中item的背景或边框颜色

shape_bg_listview.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="180"
        android:endColor="#FFCCCCCC"
        android:startColor="@android:color/white" />

    <stroke
        android:width="1px"
        android:color="@android:color/transparent" />

    <solid android:color="@android:color/background_light" />

    <corners
        android:bottomLeftRadius="10px"
        android:bottomRightRadius="10px"
        android:topLeftRadius="10px"
        android:topRightRadius="10px" />

</shape>

再就自定义listview圆角的类

CornerListView.java



/**
 * 圆角ListView示例
 * @Description: 圆角ListView
 * @FileName: CornerListView.java 
 * @author XIAJUN
 * @Date 2012-5-25 下午18:05:19 
 * @Version V1.0
 */
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_shape);
	                }
                }

                break;
        case MotionEvent.ACTION_UP:
                break;
        }
        
        return super.onInterceptTouchEvent(ev);
    }
}

最后看一下Activity的代码:

SettingActivity.java



public class SettingActivity extends Activity{


	
	private CornerListView lvState,lvAccount,lvSystem,lvExit;
	private SimpleAdapter stateAdapter,accountAdapter,systemAdapter,exitAdapter;
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		
		getStateData();
		getAccountData();
		getSystemData();
		getExitData();
		initView();
		initAdapter();

	}
	
	private void initView(){
		setContentView(R.layout.more_settings);
		lvState=(CornerListView)findViewById(R.id.lv_state);
		lvAccount=(CornerListView)findViewById(R.id.lv_account);
		lvSystem=(CornerListView)findViewById(R.id.lv_system);
		lvExit=(CornerListView)findViewById(R.id.lv_exit);
		lvState.setOnItemClickListener(new StateOnItemListSelectedListener());
		lvAccount.setOnItemClickListener(new AccountOnItemListSelectedListener());
		lvSystem.setOnItemClickListener(new SystemOnItemListSelectedListener());
		lvExit.setOnItemClickListener(new ExitOnItemListSelectedListener());
	}
	
	private void initAdapter(){
		stateAdapter = new SimpleAdapter(this,getStateData(),R.layout.state_item,
                new String[]{"state_title","state_context","state_right"},
                new int[]{R.id.tv_state_title,R.id.tv_state_context,R.id.iv_state_right});
		
		lvState.setAdapter(stateAdapter);
		
		accountAdapter = new SimpleAdapter(this,getAccountData(),R.layout.account_item,
                new String[]{"account_title","account_right"},
                new int[]{R.id.tv_account_title,R.id.iv_account_right});
		
		lvAccount.setAdapter(accountAdapter);
		
		systemAdapter = new SimpleAdapter(this,getSystemData(),R.layout.system_item,
                new String[]{"system_title","system_right"},
                new int[]{R.id.tv_system_title,R.id.iv_system_right});
		
		lvSystem.setAdapter(systemAdapter);
		
		exitAdapter = new SimpleAdapter(this,getExitData(),R.layout.exit_item,
                new String[]{"exit_title","exit_right"},
                new int[]{R.id.tv_exit_title,R.id.iv_exit_right});
		
		lvExit.setAdapter(exitAdapter);
	}
	
	private class StateOnItemListSelectedListener implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (position) {
			case 0:
				
				break;
			case 1:
				
				break;
			case 2:
				
				break;
			case 3:
	
				break;
			default:
				break;
			}
		}
	}
	
	private class AccountOnItemListSelectedListener implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (position) {
			case 0:
				
				break;
			case 1:
				
				break;
			default:
				break;
			}
			
		}
	}
	
	private class SystemOnItemListSelectedListener implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (position) {
			case 0:
				
				break;
			case 1:
				
				break;
			case 2:
				
				break;
			case 3:
	
				break;
			case 4:
				
				break;
			case 5:
				
				break;
			case 6:
	
				break;
			default:
				break;
			}
		}
	}
	
	private class ExitOnItemListSelectedListener implements OnItemClickListener{
		@Override
		public void onItemClick(AdapterView<?> parent, View view, int position,
				long id) {
			switch (position) {
			case 0:
				
				break;
			case 1:
				
				break;
			default:
				break;
			}
		}
	}
	
	private List<Map<String, Object>> getStateData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
 
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("state_title", "隐身模式");
        map.put("state_context", "对所有人可见");
        map.put("state_right", R.drawable.arrows_right);
        list.add(map);
 
        map = new HashMap<String, Object>();
        map.put("state_title", "消息提醒");
        map.put("state_context", "");
        map.put("state_right", R.drawable.arrows_right);
        list.add(map);
 
        map = new HashMap<String, Object>();
        map.put("state_title", "静音时段");
        map.put("state_context", "关闭");
        map.put("state_right", R.drawable.arrows_right);
        list.add(map);
        
        map = new HashMap<String, Object>();
        map.put("state_title", "定位服务");
        map.put("state_context", "");
        map.put("state_right", R.drawable.arrows_right);
        list.add(map);
        return list;
    }
	
	private List<Map<String, Object>> getAccountData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
 
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("account_title", "修改密码");
        map.put("account_right", R.drawable.arrows_right);
        list.add(map);
 
        map = new HashMap<String, Object>();
        map.put("account_title", "黑名单");
        map.put("account_right", R.drawable.arrows_right);
        list.add(map);
        return list;
    }
	
	private List<Map<String, Object>> getSystemData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
 
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("system_title", "分享给好友");
        map.put("system_right", R.drawable.arrows_right);
        list.add(map);
 
        map = new HashMap<String, Object>();
        map.put("system_title", "意见反馈");
        map.put("system_right", R.drawable.arrows_right);
        list.add(map);
 
        map = new HashMap<String, Object>();
        map.put("system_title", "版本信息");
        map.put("system_right", R.drawable.arrows_right);
        list.add(map);
        
        map = new HashMap<String, Object>();
        map.put("system_title", "系统设置");
        map.put("system_right", R.drawable.arrows_right);
        list.add(map);
        
        map = new HashMap<String, Object>();
        map.put("system_title", "检查更新");
        map.put("system_right", R.drawable.arrows_right);
        list.add(map);
        
        map = new HashMap<String, Object>();
        map.put("system_title", "给xx评分");
        map.put("system_right", R.drawable.arrows_right);
        list.add(map);
        
        map = new HashMap<String, Object>();
        map.put("system_title", "关于xx");
        map.put("system_right", R.drawable.arrows_right);
        list.add(map);
        return list;
    }
	
	private List<Map<String, Object>> getExitData() {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
 
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("exit_title", "注销");
        map.put("exit_right", R.drawable.arrows_right);
        list.add(map);
 
        map = new HashMap<String, Object>();
        map.put("exit_title", "退出");
        map.put("exit_right", R.drawable.arrows_right);
        list.add(map);
        return list;
    }
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		Intent intent=new Intent(SettingActivity.this, IndexActivity.class);
    	startActivity(intent);
		return super.onKeyDown(keyCode, event);
		
	}
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android ListView中添加圆角边框,可以使用以下步骤: 1. 创建一个drawable资源文件,例如“listview_border.xml”,并在其中定义一个shape元素,设置圆角和边框属性。 2. 在ListView的布局文件中,将背景属性设置为刚刚创建的drawable资源文件。 3. 在ListView的适配器中,为每个列表项设置背景颜色或背景图片,以便它们显示圆角边框。 以下是一个示例drawable资源文件的代码: ``` <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="@android:color/white" /> <corners android:radius="10dp" /> <stroke android:width="2dp" android:color="@android:color/black" /> </shape> ``` 在ListView的布局文件中,可以这样设置背景属性: ``` <ListView android:id="@+id/listview" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/listview_border" /> ``` 在适配器中,可以这样设置每个列表项的背景: ``` @Override public View getView(int position, View convertView, ViewGroup parent) { // inflate the layout for each list item View listItemView = convertView; if (listItemView == null) { listItemView = LayoutInflater.from(getContext()).inflate( R.layout.list_item, parent, false); } // set the background color or image for this list item listItemView.setBackgroundResource(R.drawable.list_item_background); // set the text and image for this list item TextView textView = (TextView) listItemView.findViewById(R.id.textview); ImageView imageView = (ImageView) listItemView.findViewById(R.id.imageview); textView.setText(getItem(position).getText()); imageView.setImageResource(getItem(position).getImageResource()); return listItemView; } ``` 在这个示例中,我们为每个列表项设置了一个名为“list_item_background”的drawable资源文件,它定义了一个圆角矩形的背景。您可以根据自己的需要更改此文件的属性。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值