android - Custom Views only showing up once -


i've made simple customview, gray rectangle arbitrary amount of red markings inside rectangle marked percentages.

public class demoview extends view { private shapedrawable mdrawable; private arraylist<shapedrawable> mmarks;  public demoview(context context, int[] marks) {     super(context);     int x = 0;     int y = 0;     int width = 100;     int height = 10;     // timeline empty      mdrawable = new shapedrawable(new rectshape());     mdrawable.getpaint().setcolor(color.gray);     mdrawable.setbounds(x, y, x + width, y + height);     // add marks     if (marks != null && marks.length % 2 == 0) {         mmarks = new arraylist<shapedrawable>(marks.length / 2);         shapedrawable mark;         (int = 1; < marks.length; = + 2) {             mark = new shapedrawable(new rectshape());             mark.getpaint().setcolor(color.red);             mark.setbounds(x + marks[i - 1], y, x + marks[i], y + height);             mmarks.add(mark);         }     } }  @override protected void ondraw(canvas canvas) {     super.ondraw(canvas);     mdrawable.draw(canvas);     if (mmarks != null)         (shapedrawable mark : mmarks)             mark.draw(canvas); } 

}

however can't figure out how make use of view. each time try add more 1 of view in linearlayout or relativelayout, see 1 of views.

xml:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/llayout" > <textview   android:layout_width="fill_parent"  android:layout_height="wrap_content"  android:text="@string/hello" /> </linearlayout> 

layout code:

public void oncreate(bundle savedinstancestate) {     super.oncreate(savedinstancestate);     setcontentview(r.layout.main);     linearlayout ll = (linearlayout) findviewbyid(r.id.llayout);     demoview = new demoview(this, new int[]{10,15,35,60});     demoview.setid(id_num++);     ll.addview(demoview);     demoview2 = new demoview(this, new int[]{0,1,3,6});     demoview2.setid(id_num++);     ll.addview(demoview2);     demoview3 = new demoview(this, new int[]{25,60});     demoview3.setid(id_num++);     ll.addview(demoview3);     demoview4 = new demoview(this, new int[]{15,60});     demoview4.setid(id_num++);     ll.addview(demoview4);  } 

results in:

result of linearlayout code above..

is wrong route take? missing obvious key using view multiple times? if not correct route there other method making custom shape? perhaps extending rectshape?

you may want watch this.

romain explains how custom views there.


Comments

Popular posts from this blog

c# - SharpSVN - How to get the previous revision? -

c++ - Is it possible to compile a VST on linux? -

url - Querystring manipulation of email Address in PHP -