圆角背景的ListView

圆角背景的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>



ListView子项的圆角效果,也可通过以下实现,转自http://www.eoeandroid.com/thread-102663-1-1.html:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#253853"
    >
    <ListView android:id="@+id/list" 
        android:layout_width="match_parent" 
        android:layout_height="match_parent"
		android:layout_marginBottom="10dp"          
        android:cacheColorHint="#00000000"
        android:divider="#2A4562"
        android:dividerHeight="4px"
        android:listSelector="#264365"
        android:drawSelectorOnTop="false"
        />
</LinearLayout>


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="fill_parent"
	android:background="@drawable/selector"
	>
	<ImageView android:id="@+id/img"
		android:layout_width="wrap_content"
		android:layout_height="wrap_content"                      
		android:layout_gravity="center_vertical"
		android:layout_marginLeft="20dp" 
		/>
	<LinearLayout android:orientation="vertical" 
		android:layout_width="fill_parent"  
		android:layout_height="wrap_content">                                                      
		<TextView android:text="data" android:id="@+id/title"
			android:layout_width="fill_parent"
			android:layout_height="wrap_content"
			android:gravity="center_vertical"
			android:layout_marginLeft="20dp"
			android:layout_marginTop="20dp"
			android:textSize="14sp"
			android:textStyle="bold"
			android:textColor="@color/black"
			/>
    </LinearLayout>
</LinearLayout>


selector.xml的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
	<item android:state_selected="true">
		<shape>
			<gradient android:angle="270" android:startColor="#A5D245"
				android:endColor="#99BD4C" />
			<padding android:left="15dip" android:top="20dip"
				android:right="15dip" android:bottom="20dip" />
			<size android:width="320dip" android:height="60dip" />
			<corners android:radius="8dip" />
		</shape>
	</item>
	<item android:state_pressed="true">
		<shape>
			<gradient android:angle="270" android:endColor="#99BD4C"
				android:startColor="#A5D245" />
			<padding android:left="15dp" android:top="20dp"
				android:right="15dp" android:bottom="20dp" />
			<size android:height="60dp" android:width="320dp" />
			<corners android:radius="8dp" />
		</shape>
	</item>
	<item>
		<shape>
			<gradient android:angle="270" android:endColor="#A8C3B0"
				android:startColor="#C6CFCE" />
			<padding android:left="15dp" android:top="20dp"
				android:right="15dp" android:bottom="20dp" />
			<size android:height="60dp" android:width="320dp" />
			<corners android:radius="8dp" />
		</shape>
	</item>
</selector>


效果图如下:

圆角背景的ListView

圆角背景的ListView

你可能感兴趣的:(android,ListView,圆角)