The examples in this article describe the way Android implements ListView and item with borders. Share to everyone for your reference, specific as follows:
There are two ways to add a border around ListView and item:
1. Paste a background picture with a border effect
2. Customizing the Draw method
The first method consumes more system resources than the second method, but is simple enough to require only one diagram to be set to the background of the corresponding control, while the second is better.
This is the implementation of the ListView and item with a border, so write a simple demo learning
Let's see how the demo works.
The following is the main code, mainly used to Canvas.drawline (...) The code is simple, and I'm not going to nag.
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) {super (context);
Public Borderlistview (context, AttributeSet attrs) {Super (context, attrs);
@Override protected void OnDraw (Canvas Canvas) {Float width = getwidth ();
Float height= getheight (); int linewidth = 10;
Line width 10 pixel int graycolor = Color.gray;
Paint mlinepaint = new Paint ();
Mlinepaint.setcolor (Graycolor);
Mlinepaint.setstyle (Paint.Style.STROKE);
Mlinepaint.setantialias (TRUE);
Mlinepaint.setstrokewidth (linewidth);
Draw around the border note the following LINEWIDTH/2 without words around the line may not be as thick 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 to add dashes and red lines
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 {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);
Draw two straight lines Canvas.drawline (leftframepos+20, 0f, leftframepos+20, GetHeight (), mlinepaint);
Canvas.drawline (leftframepos+25, 0f, leftframepos+25, GetHeight (), mlinepaint);
Draw dashed 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 a stub return list.size ();
@Override public Object getitem (int i) {//TODO auto-generated a stub return list.get (i);
@Override public long Getitemid (int i) {//TODO auto-generated a 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;
}
}
In addition, in the time of the demo, the error was reported, caused By:java.lang.NoSuchMethodException:BorderListView (context,attributeset)
Add a constructor to the Borderlistview class to fix
Public Borderlistview (context, AttributeSet attrs)
{
Super (context, attrs);
}
For more information on Android-related content readers can view the site: "The activity of Android programming skills Summary", "Android Resources Operating Skills Summary", "Android File Operating skills summary", " Android Operation SQLite Database skills Summary, "Android operation JSON format Data Skills summary", "Android Database Operation skills Summary", "Android programming development of SD card Operation Summary", "Android Development introduction and Advanced Course", The Android View view tips summary and the Android Control usage summary
I hope this article will help you with the Android program.