Android實(shí)現(xiàn)帶有邊框的ListView和item的方法
本文實(shí)例講述了Android實(shí)現(xiàn)帶有邊框的ListView和item的方法。分享給大家供大家參考,具體如下:
想為L(zhǎng)istView和item四周添加邊框有兩種方法:
1.貼一張帶有邊框效果的背景圖
2.自定義Draw的方法
第一種方法較第二種方法更耗系統(tǒng)資源,但是用法簡(jiǎn)單,只需要一張圖設(shè)置為相應(yīng)控件的背景即可,而第二種靈活性好些。
這次是實(shí)現(xiàn)帶有邊框的ListView和item,為此寫(xiě)個(gè)簡(jiǎn)單Demo 學(xué)習(xí)學(xué)習(xí)
先看下Demo運(yùn)行效果吧

下面是主要代碼,主要是用到Canvas.drawLine(...)代碼簡(jiǎn)單,我就不啰嗦了
BorderListView.Java
package com.borderlistview.manymore13;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ListView;
public class BorderListView extends ListView{
public BorderListView(Context context) {
super(context);
}
public BorderListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
@Override
protected void onDraw(Canvas canvas) {
float width = getWidth();
float height= getHeight();
int lineWidth = 10; // 線寬十個(gè)像素
int grayColor = Color.GRAY;
Paint mLinePaint = new Paint();
mLinePaint.setColor(grayColor);
mLinePaint.setStyle(Paint.Style.STROKE);
mLinePaint.setAntiAlias(true);
mLinePaint.setStrokeWidth(lineWidth);
// 畫(huà)四周的邊框 注意下面的 lineWidth/2 不加的話四周的線可能不一樣粗
canvas.drawLine(0f, 0+lineWidth/2, width, 0+lineWidth/2, mLinePaint);
canvas.drawLine(width-lineWidth/2, 0, width-lineWidth/2, height, mLinePaint);
canvas.drawLine(width-lineWidth/2, height-lineWidth/2, 0, height-lineWidth/2, mLinePaint);
canvas.drawLine(0+lineWidth/2, height, 0+lineWidth/2, 0,mLinePaint);
super.onDraw(canvas);
}
}
ListViewItem.java ListView的item 添加虛線和紅線
package com.borderlistview.manymore13;
import com.manymore13.MyListview.R;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.DashPathEffect;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ListViewItem extends RelativeLayout{
private View viewHolder;
private TextView tvEventName;
private Context c;
private FrameLayout leftFrame;
public ListViewItem(Context context) {
super(context);
LayoutInflater flater = LayoutInflater.from(context);
viewHolder = flater.inflate(R.layout.item, this);
getViewAndSetClick();
c = context;
}
private void getViewAndSetClick()
{
tvEventName = (TextView)viewHolder.findViewById(R.id.eventName);
leftFrame = (FrameLayout)viewHolder.findViewById(R.id.frame);
}
public void setEventName(String name)
{
tvEventName.setText(name);
}
public void updateView()
{
this.postInvalidate();
}
@Override
protected void dispatchDraw(Canvas canvas) {
super.dispatchDraw(canvas);
Resources res = getResources();
int grayColor = Color.GRAY;
int redColor = res.getColor(R.color.red);
int leftFramepos = leftFrame.getRight();
Paint mLinePaint = new Paint();
mLinePaint.setColor(redColor);
mLinePaint.setStyle(Paint.Style.STROKE);
mLinePaint.setStrokeWidth(2);
//畫(huà)兩條直線
canvas.drawLine(leftFramepos+20, 0f, leftFramepos+20, getHeight(), mLinePaint);
canvas.drawLine(leftFramepos+25, 0f, leftFramepos+25, getHeight(), mLinePaint);
// 畫(huà)虛線
mLinePaint.setColor(grayColor);
DashPathEffect effect = new DashPathEffect(new float[] { 5,5, 5, 5, 5}, 3);
mLinePaint.setAntiAlias(true);
mLinePaint.setPathEffect(effect);
canvas.drawLine(0, getHeight(), getWidth(), getHeight(), mLinePaint);
}
}
MyBaseAdaper.java
package com.borderlistview.manymore13;
import java.util.List;
import android.content.Context;
import android.os.Handler;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class MyBaseAdaper extends BaseAdapter{
private List<String> list;
private Context c;
MyBaseAdaper(Context c, List<String> list)
{
this.list = list;
this.c = c;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return list.size();
}
@Override
public Object getItem(int i) {
// TODO Auto-generated method stub
return list.get(i);
}
@Override
public long getItemId(int i) {
// TODO Auto-generated method stub
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewgroup) {
ListViewItem itemView = null ;
if(view == null){
itemView = new ListViewItem(c);
}else{
itemView = (ListViewItem)view;
}
itemView.setEventName(list.get(i));
return itemView;
}
}
另外,在寫(xiě)本次Demo的時(shí)候報(bào)了錯(cuò)誤,有錯(cuò)就改 Caused by: java.lang.NoSuchMethodException:BorderListView(Context,AttributeSet)
在 BorderListView類中加一個(gè)構(gòu)造函數(shù) 搞定
public BorderListView(Context context, AttributeSet attrs)
{
super(context, attrs);
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android編程之a(chǎn)ctivity操作技巧總結(jié)》、《Android資源操作技巧匯總》、《Android文件操作技巧匯總》、《Android操作SQLite數(shù)據(jù)庫(kù)技巧總結(jié)》、《Android操作json格式數(shù)據(jù)技巧總結(jié)》、《Android數(shù)據(jù)庫(kù)操作技巧總結(jié)》、《Android編程開(kāi)發(fā)之SD卡操作方法匯總》、《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android TextView設(shè)置背景色與邊框的方法詳解
- android開(kāi)發(fā)教程之view組件添加邊框示例
- Android 去掉自定義dialog的白色邊框的簡(jiǎn)單方法
- Android中EditText如何去除邊框添加下劃線
- Android 圓角邊框的實(shí)現(xiàn)方式匯總
- android dialog邊框去除白色邊框?qū)崿F(xiàn)思路及代碼
- Android TextView(圓?。┻吙蚝捅尘皩?shí)例詳解
- Android實(shí)現(xiàn)代碼畫(huà)虛線邊框背景效果
- Android布局實(shí)現(xiàn)圓角邊框效果
- Android實(shí)現(xiàn)為GridView添加邊框效果
相關(guān)文章
Android中Binder詳細(xì)學(xué)習(xí)心得
這篇文章主要介紹了Android中Binder詳細(xì)學(xué)習(xí)心得,并分析了Binder的詳細(xì)用法,需要的朋友參考下吧。2018-01-01
Android應(yīng)用中ListView利用OnScrollListener分頁(yè)加載數(shù)據(jù)
這篇文章主要介紹了Android應(yīng)用中ListView利用OnScrollListener分頁(yè)加載數(shù)據(jù)的方法,包括對(duì)OnScrollListener事件順序次數(shù)的分析,需要的朋友可以參考下2016-03-03
Android使用ShareSDK實(shí)現(xiàn)應(yīng)用分享的功能
這篇文章主要為大家詳細(xì)介紹了Android使用ShareSDK實(shí)現(xiàn)應(yīng)用分享的功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-05-05
Android Framework Application Framework層簡(jiǎn)單介紹
這篇文章主要介紹了 Android Framework Application Framework層簡(jiǎn)單介紹的相關(guān)資料,需要的朋友可以參考下2016-11-11
Android實(shí)現(xiàn)靜態(tài)廣播監(jiān)聽(tīng)器的方法
這篇文章主要介紹了Android實(shí)現(xiàn)靜態(tài)廣播監(jiān)聽(tīng)器的方法,涉及Android的廣播機(jī)制與記錄監(jiān)聽(tīng)廣播信息的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-07-07
sqlite查詢結(jié)果在listview中展示的實(shí)現(xiàn)
下面小編就為大家?guī)?lái)一篇sqlite查詢結(jié)果在listview中展示的實(shí)現(xiàn)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-04-04
android 實(shí)現(xiàn)控件左右或上下抖動(dòng)教程
這篇文章主要介紹了android 實(shí)現(xiàn)控件左右或上下抖動(dòng)教程,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-03-03
Android使用注解進(jìn)行代碼檢查的實(shí)現(xiàn)方法
這篇文章主要介紹了Android如何使用注解進(jìn)行代碼檢查,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09

