先在drawable 加入4個xml檔案
list_top_selector.xml (第一筆資料)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<stroke android:width="1dp" android:color="@color/gray" />
<solid android:color="@color/gray" />
<corners android:topLeftRadius="8dp" android:topRightRadius="8dp" />
</shape></item>
<item><shape>
<solid android:color="@color/white" />
<corners android:topLeftRadius="8dp" android:topRightRadius="8dp" />
</shape></item>
</selector>
list_center_selector.xml(中間的資料)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<stroke android:width="1dp" android:color="@color/gray" />
<solid android:color="@color/gray" />
</shape></item>
<item><shape>
<solid android:color="@color/white" />
</shape></item>
</selector>
list_bottom_selector.xml(最後一筆資料)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<stroke android:width="1dp" android:color="@color/gray" />
<solid android:color="@color/gray" />
<corners android:bottomLeftRadius="@dimen/corner_radius" android:bottomRightRadius="@dimen/corner_radius" />
</shape></item>
<item><shape>
<solid android:color="@color/white" />
<corners android:bottomLeftRadius="@dimen/corner_radius" android:bottomRightRadius="@dimen/corner_radius" />
</shape></item>
</selector>
default_selector.xml(只有一筆資料)
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"><shape>
<stroke android:width="1dp" android:color="@color/gray" />
<solid android:color="@color/gray" />
<corners android:radius="8dp" />
</shape></item>
<item><shape>
<stroke android:width="1dp" android:color="@color/gray" />
<solid android:color="@color/white" />
<corners android:radius="8dp" />
</shape></item>
</selector>
接者在你的ListView 加上紅色的那兩行
<?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"
android:orientation="vertical"
android:padding="10dip" >
<ListView
android:id="@+id/lstAccount"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:listSelector="#00000000" >
</ListView>
</LinearLayout>
最後一步就是在你自訂的Adapter判斷要顯示的drawable,如下紅色字體
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.account_item, null);
holder = new ViewHolder();
holder.imgDel = (ImageButton) convertView
.findViewById(R.id.imgDel);
holder.txtAlias = (TextView) convertView
.findViewById(R.id.txtAlias);
holder.txtType = (TextView) convertView
.findViewById(R.id.txtType);
holder.txtNumber = (TextView) convertView
.findViewById(R.id.txtNumber);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if (accounts.size() == 1){
convertView.setBackgroundResource(R.drawable.default_selector);
}else{
if (position == 0){
convertView.setBackgroundResource(R.drawable.list_top_selector);
}else if (position == accounts.size()-1){
convertView.setBackgroundResource(R.drawable.list_bottom_selector);
}else{
convertView.setBackgroundResource(R.drawable.list_center_selector);
}
}
return convertView;
}
}
留言列表