圆角背景的ListView


先定义一张圆角的图片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:startColor="@android:color/white"  
       android:endColor="#FFCCCCCC"  
       android:angle="180" 
       />
       <stroke android:width="0px" 
       android:color="@android:color/white" 
       /> 
       <solid android:color="@android:color/white" 
       /> 
       <corners 
       android:bottomRightRadius="20px" 
       android:bottomLeftRadius="20px" 
       android:topLeftRadius="20px" 
       android:topRightRadius="20px" 
       />
</shape> 

然后在ListView中引用它,注意android:listSelector为了去掉选中效果:
<ListView android:id="@+id/list" 
		android:layout_width="fill_parent" 
		android:layout_height="wrap_content"
		android:layout_margin="20.0dip"
		android:cacheColorHint="@null" 
                android:listSelector="@drawable/shape_bg_listview"
		android:background="@drawable/shape_bg_listview"
		/>

代码:
public class App extends Activity {
	private ListView mListView = null;
	
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        ArrayList<String> list =new ArrayList<String>();
        list.add("1");
        list.add("2");
        list.add("3");
        mListView =  (ListView)findViewById(R.id.list);
        mListView.setAdapter(new AppAdapter(this,list));
    }
    
    class AppAdapter extends BaseAdapter{

    	Context context;
    	ArrayList<String> list;
    	AppAdapter(Context context,ArrayList<String> list){
    		this.context=context;
    		this.list=list;
    	}
		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return list.size();
		}

		@Override
		public Object getItem(int position) {
			// TODO Auto-generated method stub
			return list.get(position);
		}

		@Override
		public long getItemId(int position) {
			// TODO Auto-generated method stub
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			// TODO Auto-generated method stub
			TextView tv;
			if(convertView==null){
				convertView=LayoutInflater.from(context).inflate(R.layout.simple_item_2, null);
			}
			tv=(TextView)convertView.findViewById(R.id.name);
			tv.setText(list.get(position));
			return convertView;
		}
    	
    }
}


simple_item_2.xml定义如下:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
	xmlns:android="http://schemas.android.com/apk/res/android"
	android:layout_width="fill_parent"
	android:layout_height="wrap_content"
	>
	<TextView
		android:id="@+id/name"
		android:textSize="17.0sp"
		android:textColor="@android:color/black"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginLeft="20.0dip"
		android:layout_alignParentLeft="true"
		android:layout_centerVertical="true"
		>
	</TextView>
	<ImageView
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"
		android:layout_marginRight="20.0dip"
		android:src="@drawable/arrow"
		android:layout_alignParentRight="true"
		android:layout_centerVertical="true"
		>
	</ImageView>
</RelativeLayout>


App列表之圆角ListView(续)
http://www.cnblogs.com/qianxudetianxia/archive/2011/12/11/2283794.html

你可能感兴趣的:(xml,ITeye)