How to implement a custom layout, not extending existing one

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How to implement a custom layout, not extending existing one

Postby mikee.tellme » Mon Jan 07, 2008 9:09 am

hi, everyone!
I just want to implement a custom layout, but the layout is not a composition of current layouts or a modification version by extending existing layouts , what I want is to get the knowledge of existing layout's(e.g. LinearLayout) implementation.
And I've read the android docs:
http://code.google.com/android/toolbox/ ... nents.html,
but I've not found the right way to display my custom layout(extends ViewGroup), It must be something wrong in my code or my misunderstanding to the doc.
Another strange problem, when I set a breakpoint in the onDraw(), debug the app, I found that the onDraw() will not be invoked.

Is there anybody done the work like this?
Any reply will be appreciated and thankful.

My test Code:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. import java.util.ArrayList;
  3.  
  4.  
  5.  
  6. import android.content.Context;
  7.  
  8. import android.graphics.Canvas;
  9.  
  10. import android.view.View;
  11.  
  12. import android.view.ViewGroup;
  13.  
  14.  
  15.  
  16. public class MyLayout extends ViewGroup {
  17.  
  18.         ArrayList<View> adapter;
  19.  
  20.  
  21.  
  22.         public MyLayout(Context context) {
  23.  
  24.                 super(context);
  25.  
  26.         }
  27.  
  28.  
  29.  
  30.         public void addView(View child, LayoutParams params) {
  31.  
  32.                 if (adapter == null) {
  33.  
  34.                         adapter = new ArrayList<View>();
  35.  
  36.                 }
  37.  
  38.                 adapter.add(child);
  39.  
  40.                 super.addView(child, params);
  41.  
  42.         }
  43.  
  44.  
  45.  
  46.         @Override
  47.  
  48.         public void removeView(View view) {
  49.  
  50.                 if (adapter != null) {
  51.  
  52.                         adapter.remove(view);
  53.  
  54.                 }
  55.  
  56.                 super.removeView(view);
  57.  
  58.         }
  59.  
  60.  
  61.  
  62.         @Override
  63.  
  64.         protected void onDraw(Canvas canvas) {
  65.  
  66.                 // TODO Auto-generated method stub
  67.  
  68.                 super.onDraw(canvas);
  69.  
  70.         }
  71.  
  72.  
  73.  
  74.         @Override
  75.  
  76.         protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  77.  
  78.                 // TODO Auto-generated method stub
  79.  
  80. //              super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  81.  
  82.                 setMeasuredDimension(100, 200);
  83.  
  84.         }
  85.  
  86.  
  87.  
  88.         protected void onLayout(boolean changed, int wl, int wt, int l, int t,
  89.  
  90.                         int r, int b) {
  91.  
  92.                 int size = adapter.size();
  93.  
  94.                 int width = r - l, height = b - t, lastTop = 0;
  95.  
  96.                 View lastV = null;
  97.  
  98.                 for (int i = 0; i < size; i++) {
  99.  
  100.                         int left, top, right, bottom;
  101.  
  102.                         if (lastV != null) {
  103.  
  104.                                 lastTop += lastV.getMeasuredHeight();
  105.  
  106.                         }
  107.  
  108.                         View item = (View) adapter.get(i);
  109.  
  110.                         left = 0;
  111.  
  112.                         top = lastTop;
  113.  
  114.                         item.measure(MeasureSpec.AT_MOST, MeasureSpec.AT_MOST);
  115.  
  116.                         right = left + item.getMeasuredWidth();
  117.  
  118.                         bottom = top + item.getMeasuredHeight();
  119.  
  120.                         item.layout(wl, wt + lastTop, left, top, right, bottom);
  121.  
  122.                         lastV = item;
  123.  
  124.                 }
  125.  
  126.         }
  127.  
  128. }
  129.  
  130.  
  131.  
  132. ======================================================
  133.  
  134. import android.app.Activity;
  135.  
  136. import android.os.Bundle;
  137.  
  138. import android.view.ViewGroup;
  139.  
  140. import android.widget.TextView;
  141.  
  142.  
  143.  
  144. public class MyLayoutActivity extends Activity {
  145.  
  146.  
  147.  
  148.         protected void onCreate(Bundle icicle) {
  149.  
  150.                 super.onCreate(icicle);
  151.  
  152.                 MyLayout ml = new MyLayout(this);
  153.  
  154.  
  155.  
  156.                 for (int i = 0; i < 10; i++) {
  157.  
  158.                         TextView tv = new TextView(this);
  159.  
  160.                         tv.setText("str-" + i);
  161.  
  162.                         ml.addView(tv, new ViewGroup.LayoutParams(
  163.  
  164.                                         ViewGroup.LayoutParams.WRAP_CONTENT,
  165.  
  166.                                         ViewGroup.LayoutParams.WRAP_CONTENT));
  167.  
  168.                 }
  169.  
  170.                 setContentView(ml);
  171.  
  172.         }
  173.  
  174. }
Parsed in 0.039 seconds, using GeSHi 1.0.8.4



Mikee
mikee.tellme
Once Poster
Once Poster
 
Posts: 1
Joined: Mon Jan 07, 2008 7:23 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: Majestic-12 [Bot] and 5 guests