Android實(shí)現(xiàn)圓角ListView效果
在項(xiàng)目開(kāi)發(fā)中我們可能會(huì)碰到圓角ListView效果,因?yàn)橹苯堑目雌饋?lái)確實(shí)不那么雅觀,可能大家會(huì)想到用圖片實(shí)現(xiàn),試想上中下要分別做三張圖片,這樣做太繁瑣,這時(shí)使用shape來(lái)實(shí)現(xiàn)不失為一種更好的實(shí)現(xiàn)方式。
先看一下Android 中Shape的使用方法:
solid:實(shí)心,就是填充的意思
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:width="2dp" 描邊的寬度,android:color 描邊的顏色。
我們還可以把描邊弄成虛線的形式,設(shè)置方式為:
android:dashWidth="5dp"
android:dashGap="3dp"
其中android:dashWidth表示'-'這樣一個(gè)橫線的寬度,android:dashGap表示之間隔開(kāi)的距離。
corners:圓角
android:radius為角的弧度,值越大角越圓。
當(dāng)然,這里并不是說(shuō)這種圓角的列表一段是ListView來(lái)實(shí)現(xiàn)的,可能是由多個(gè)LinearLayout/RelativeLayout疊起來(lái)的。這個(gè)就看你怎么取舍了;如果列表項(xiàng)固定不怎么變化可以采取后者來(lái)實(shí)現(xiàn)比較好,如果需要?jiǎng)討B(tài)變化那么使用ListView來(lái)實(shí)現(xiàn)更優(yōu)。
下面來(lái)定義一下ListView只有一項(xiàng)時(shí)的背景(上下兩個(gè)角都是圓角) 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第一項(xiàng)的背景(上面是圓角,下面是直角) 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最后一項(xiàng)的背景(上面是直角,下面是圓角) 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中間項(xiàng)的背景(上下都是直角) 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>
接下來(lái)先看看Adapter的實(shí)現(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){ //只有一項(xiàng)
convertView.setBackgroundResource(R.drawable.app_list_corner_round);
}else{ //第一項(xiàng)
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:id="@+id/listview_item_textview" 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:id="@+id/listview_item_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/arrow" android:layout_alignParentRight="true" android:layout_centerVertical="true" /> </RelativeLayout>
最后看看主界面Activity的實(shí)現(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:id="@+id/mListView" 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>
最后看看實(shí)現(xiàn)的效果

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android WebView基礎(chǔ)應(yīng)用詳解
這篇文章主要為大家介紹了Android中WebView這一控件的基礎(chǔ)應(yīng)用,例如:播放音樂(lè),播放視頻等,文中的示例代碼講解詳細(xì),對(duì)于我們了解WebView很有幫助,需要的同學(xué)可以學(xué)習(xí)一下2021-12-12
關(guān)于Android的 DiskLruCache磁盤(pán)緩存機(jī)制原理
DiskLruCache是一種管理數(shù)據(jù)存儲(chǔ)的技術(shù),單從Cache的字面意思也可以理解到,"Cache","高速緩存";今天我們來(lái)從源碼上分析下DiskLruCache;關(guān)于Android LruCache的緩存機(jī)制原理,需要的朋友可以參考下面文章的具體內(nèi)容2021-09-09
Java實(shí)現(xiàn)Andriod帶看括弧的計(jì)算器代碼
這篇文章主要介紹了Java實(shí)現(xiàn)Andriod帶看括弧的計(jì)算器代碼的相關(guān)資料,需要的朋友可以參考下2016-03-03
Android Handler runWithScissors 梳理流程解析
這篇文章主要為大家介紹了Android Handler runWithScissors 梳理流程解析,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10
一看就喜歡的loading動(dòng)畫(huà)效果Android分析實(shí)現(xiàn)
一看就喜歡的loading動(dòng)畫(huà)效果Android分析實(shí)現(xiàn),絢爛的效果,相信大家一定會(huì)喜歡,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-01-01
Flutter實(shí)現(xiàn)Android滾動(dòng)懸浮效果過(guò)程
這篇文章主要介紹了Flutter實(shí)現(xiàn)Android滾動(dòng)懸浮效果,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01
Flutter實(shí)現(xiàn)仿京東商品詳情底部操作欄
這篇文章主要為大家詳細(xì)介紹了Flutter如何仿京東實(shí)現(xiàn)商品詳情底部操作欄,文中的示例代碼講解詳細(xì),具有一定的學(xué)習(xí)價(jià)值,感興趣的小伙伴可以了解一下2023-06-06
Android進(jìn)程通信之Messenger和AIDL使用詳解
本篇文章主要介紹了Android進(jìn)程通信之Messenger和AIDL使用詳解,具有一定的參考價(jià)值,有興趣的可以了解一下。2017-01-01
Android實(shí)現(xiàn)自動(dòng)掛斷電話功能的示例代碼
在開(kāi)發(fā)Android應(yīng)用時(shí),有時(shí)會(huì)遇到需要實(shí)現(xiàn)自動(dòng)掛斷電話的需求,本文將詳細(xì)介紹一下如何在Android中實(shí)現(xiàn)自動(dòng)掛斷電話的功能,需要的小伙伴可以參考一下2025-04-04

