国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

Android實現圓角ListView效果

瀏覽:91日期:2022-09-22 08:30:27

在項目開發中我們可能會碰到圓角ListView效果,因為直角的看起來確實不那么雅觀,可能大家會想到用圖片實現,試想上中下要分別做三張圖片,這樣做太繁瑣,這時使用shape來實現不失為一種更好的實現方式。

先看一下Android 中Shape的使用方法:

solid:實心,就是填充的意思

android:color指定填充的顏色

gradient:漸變

android:startColor和android:endColor分別為起始和結束顏色,ndroid:angle是漸變角度,必須為45的整數倍。另外漸變默認的模式為android:type='linear',即線性漸變,可以指定漸變為徑向漸變,android:type='radial',徑向漸變需要指定半徑android:gradientRadius='50'。

stroke:描邊

android: 描邊的寬度,android:color 描邊的顏色。我們還可以把描邊弄成虛線的形式,設置方式為:android:dashWidth='5dp'android:dashGap='3dp'其中android:dashWidth表示’-’這樣一個橫線的寬度,android:dashGap表示之間隔開的距離。

corners:圓角

android:radius為角的弧度,值越大角越圓。

當然,這里并不是說這種圓角的列表一段是ListView來實現的,可能是由多個LinearLayout/RelativeLayout疊起來的。這個就看你怎么取舍了;如果列表項固定不怎么變化可以采取后者來實現比較好,如果需要動態變化那么使用ListView來實現更優。

下面來定義一下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的實現

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://m.cgvv.com.cn/bcjs/@drawable/arrow' android:layout_alignParentRight='true' android:layout_centerVertical='true' /> </RelativeLayout>

最后看看主界面Activity的實現

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('設置 '+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='設置' 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>

最后看看實現的效果

Android實現圓角ListView效果

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持好吧啦網。

標簽: Android
相關文章:
主站蜘蛛池模板: 99精品国产免费久久国语 | 久久国产99| 久久久久99精品成人片三人毛片 | 国产精品高清一区二区 | 久久99这里只有精品国产 | 91精品免费久久久久久久久 | 国产a级三级三级三级 | 偷窥女厕国产在线视频 | 久久久精品久久久久三级 | 亚洲精品毛片久久久久久久 | 日本免费一级视频 | 国产黄页 | 亚洲欧美一区在线 | 亚洲国产欧美在线人成精品一区二区 | 一级日韩 | 亚洲一区欧美二区 | 91综合精品网站久久 | 亚洲悠悠色综合中文字幕 | 手机看片国产欧美日韩高清 | 波多野一区二区 | 国产亚洲精品aaa大片 | 特黄特a级特别特级特毛片 特黄特黄 | 精品国产一区二区三区久久 | 日韩二区三区 | 国产一区二区三区高清 | 成人亚洲欧美综合 | 日本成人中文字幕 | 国产三及 | 男人扒开腿躁女人j | 亚洲欧美一级视频 | 广东毛片 | 一区二区三区中文国产亚洲 | 加勒比一本一道在线 | 综合久久91| 亚洲精品资源网在线观看 | 草草影院永久在线观看 | 草久免费视频 | 国产一国产一有一级毛片 | 久久五| www.色片| 日韩欧美一区二区三区不卡视频 |