Android實現(xiàn)圓角ListView效果
在項目開發(fā)中我們可能會碰到圓角ListView效果,因為直角的看起來確實不那么雅觀,可能大家會想到用圖片實現(xiàn),試想上中下要分別做三張圖片,這樣做太繁瑣,這時使用shape來實現(xiàn)不失為一種更好的實現(xiàn)方式。
先看一下Android 中Shape的使用方法:
solid:實心,就是填充的意思
android:color指定填充的顏色
gradient:漸變
android:startColor和android:endColor分別為起始和結(jié)束顏色,ndroid:angle是漸變角度,必須為45的整數(shù)倍。另外漸變默認(rèn)的模式為android:type='linear',即線性漸變,可以指定漸變?yōu)閺较驖u變,android:type='radial',徑向漸變需要指定半徑android:gradientRadius='50'。
stroke:描邊
android: 描邊的寬度,android:color 描邊的顏色。我們還可以把描邊弄成虛線的形式,設(shè)置方式為:android:dashWidth='5dp'android:dashGap='3dp'其中android:dashWidth表示’-’這樣一個橫線的寬度,android:dashGap表示之間隔開的距離。
corners:圓角
android:radius為角的弧度,值越大角越圓。
當(dāng)然,這里并不是說這種圓角的列表一段是ListView來實現(xiàn)的,可能是由多個LinearLayout/RelativeLayout疊起來的。這個就看你怎么取舍了;如果列表項固定不怎么變化可以采取后者來實現(xiàn)比較好,如果需要動態(tài)變化那么使用ListView來實現(xiàn)更優(yōu)。
下面來定義一下ListView只有一項時的背景(上下兩個角都是圓角) app_list_corner_round.xml
<?xml version='1.0' encoding='utf-8'?><shape xmlns:android='http://schemas.android.com/apk/res/android' android:shape='rectangle' > <!-- 漸變 --> <gradient android:angle='270' android:endColor='@color/white' android:startColor='@color/white' /> <!-- 圓角 --> <corners android:bottomLeftRadius='4dip' android:bottomRightRadius='4dip' android:topLeftRadius='4dip' android:topRightRadius='4dip' /> </shape>
ListView第一項的背景(上面是圓角,下面是直角) app_list_corner_round_top.xml
<?xml version='1.0' encoding='utf-8'?><shape xmlns:android='http://schemas.android.com/apk/res/android' android:shape='rectangle' > <gradient android:angle='270' android:endColor='@color/white' android:startColor='@color/white' /> <corners android:topLeftRadius='@dimen/app_list_radius' android:topRightRadius='@dimen/app_list_radius' /> </shape>
ListView最后一項的背景(上面是直角,下面是圓角) app_list_corner_round_bottom.xml
<?xml version='1.0' encoding='utf-8'?><shape xmlns:android='http://schemas.android.com/apk/res/android' android:shape='rectangle' > <gradient android:angle='270' android:endColor='@color/white' android:startColor='@color/white' /> <corners android:bottomLeftRadius='@dimen/app_list_radius' android:bottomRightRadius='@dimen/app_list_radius' /> </shape>
ListView中間項的背景(上下都是直角) app_list_corner_round_center.xml
<?xml version='1.0' encoding='utf-8'?><shape xmlns:android='http://schemas.android.com/apk/res/android' android:shape='rectangle' > <gradient android:angle='270' android:endColor='@color/white' android:startColor='@color/white' /> </shape>
接下來先看看Adapter的實現(xiàn)
package com.example.roundcorner.adapter; import java.util.List; import android.content.Context;import android.view.LayoutInflater;import android.view.View;import android.view.ViewGroup;import android.widget.BaseAdapter;import android.widget.ImageView;import android.widget.TextView; import com.example.roundcorner.R;import com.example.roundcorner.entity.ListBean; public class ListAdapter extends BaseAdapter { private List<ListBean> mList; private Context mContext; public ListAdapter(Context mContext,List<ListBean> mList) { this.mList = mList; this.mContext = mContext.getApplicationContext(); } @Override public int getCount() { return this.mList.size(); } @Override public Object getItem(int position) { return this.mList.get(position); } @Override public long getItemId(int position) { return position; } @Override public int getItemViewType(int position) { // TODO Auto-generated method stub return super.getItemViewType(position); } @Override public int getViewTypeCount() { // TODO Auto-generated method stub return super.getViewTypeCount(); } @Override public View getView(int position, View convertView, ViewGroup parent) { ViewHolder holder = null; if (convertView == null) { holder = new ViewHolder(); convertView = LayoutInflater.from(this.mContext).inflate( R.layout.listview_item, null, false); holder.textView = (TextView) convertView .findViewById(R.id.listview_item_textview); holder.imageView = (ImageView) convertView .findViewById(R.id.listview_item_imageview); convertView.setTag(holder); } else { holder = (ViewHolder) convertView.getTag(); } if(position==0){ if(position == getCount()-1){ //只有一項 convertView.setBackgroundResource(R.drawable.app_list_corner_round); }else{ //第一項 convertView.setBackgroundResource(R.drawable.app_list_corner_round_top); } }else if(position == getCount()-1){ convertView.setBackgroundResource(R.drawable.app_list_corner_round_bottom); }else{ convertView.setBackgroundResource(R.drawable.app_list_corner_round_center); } ListBean lb = mList.get(position); holder.textView.setText(lb.getKey()); return convertView; } static class ViewHolder { TextView textView; ImageView imageView; }}
listview_item.xml
<?xml version='1.0' encoding='utf-8'?><RelativeLayout xmlns:android='http://schemas.android.com/apk/res/android' android:layout_width='match_parent' android:layout_height='wrap_content'> <TextView android: android:layout_width='wrap_content' android:layout_height='48dp' android:paddingLeft='10dp' android:gravity='center_vertical' android:layout_centerVertical='true' android:text='A-H' android:textColor='@color/black' android:textSize='20sp' /> <ImageView android: android:layout_width='wrap_content' android:layout_height='wrap_content' android:src='http://www.aoyou183.cn/bcjs/@drawable/arrow' android:layout_alignParentRight='true' android:layout_centerVertical='true' /> </RelativeLayout>
最后看看主界面Activity的實現(xiàn)
package com.example.roundcorner; import java.util.ArrayList;import java.util.List; import android.app.Activity;import android.os.Bundle;import android.widget.ListView; import com.example.roundcorner.adapter.ListAdapter;import com.example.roundcorner.entity.ListBean; public class MainActivity extends Activity { private List<ListBean> data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initData(); findView(); } private void findView() { ListView mListView = (ListView) findViewById(R.id.mListView); ListAdapter mAdapter = new ListAdapter(this,data); mListView.setAdapter(mAdapter); } private void initData() { data = new ArrayList<ListBean>(); for (int i = 0; i < 5; i++) { ListBean lb = new ListBean(); lb.setKey('設(shè)置 '+i); data.add(lb); } } }
activity_main.xml
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' xmlns:tools='http://schemas.android.com/tools' android:layout_width='match_parent' android:layout_height='match_parent' android:orientation='vertical' tools:context='.MainActivity' > <TextView android:layout_width='match_parent' android:layout_height='48dp' android:background='@color/white' android:gravity='center' android:text='設(shè)置' android:textSize='20sp' /> <RelativeLayout android:layout_width='match_parent' android:layout_height='match_parent' android:padding='10dp' > <ListView android: android:layout_width='match_parent' android:layout_height='wrap_content' android:background='@drawable/app_list_round' android:cacheColorHint='@android:color/transparent' android:divider='@drawable/app_list_divider' android:dividerHeight='2dip' android:padding='2dp' /> </RelativeLayout> </LinearLayout>
最后看看實現(xiàn)的效果
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
