howto create my own layout?

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

howto create my own layout?

Postby nexus7 » Wed Feb 10, 2010 2:37 pm

Hi there,

I am trying to create a subclass of LinearLayout in order to learn the ropes of layouting.

I understand that onMeasure() and onLayout() are doing the work. But I do not understand how the values have to be propagated to child views.

I tried the following, but as a result nothing is being displayed. What am I doing wrong?

Regards
- N7

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. class MyLinearView extends LinearView {
  2.  
  3. // I want all children(Buttons) have the same width.
  4.  
  5.  
  6.  
  7. @Override
  8.  
  9. protected void onLayout( boolean changed, int l, int t, int r, int b ) {
  10.  
  11.         int len = getChildCount();
  12.  
  13.         int w = ((r-l)-(len-1)*PADDING)/len;
  14.  
  15.         int pos = l;
  16.  
  17.         for( int i=0; i<len; i++ ) {
  18.  
  19.                 View c = getChildAt(i);
  20.  
  21.                 c.layout( pos, t, pos+w, b );
  22.  
  23.                 pos += w + PADDING;
  24.  
  25.         }
  26.  
  27. }
  28.  
  29.        
  30.  
  31. @Override
  32.  
  33. protected void onMeasure( int wSpec, int hSpec ) {
  34.  
  35.         int w = MeasureSpec.getSize(wSpec);
  36.  
  37.         int len = getChildCount();
  38.  
  39.         int w1 = (w-(len-1)*PADDING)/len;
  40.  
  41.         for( int i=0; i<len; i++ ) {
  42.  
  43.                 View c = getChildAt(i);
  44.  
  45.                 measureChild(c, resolveSize(w1,wSpec), hSpec);
  46.  
  47.         }
  48.  
  49.         setMeasuredDimension(resolveSize(w, wSpec),hSpec);
  50.  
  51. }
  52.  
  53. ...
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
nexus7
Freshman
Freshman
 
Posts: 2
Joined: Wed Feb 10, 2010 2:29 pm

Top

Postby nexus7 » Thu Feb 18, 2010 3:45 pm

As often, the error was simple: The parameters of layout are relative to the parent object, so they cannot be passed to the child like here.

This works perfectly:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3. protected void onLayout( boolean changed, int l, int t, int r, int b ) {
  4.  
  5.      int len = getChildCount();
  6.  
  7.      int w = ((r-l)-(len-1)*PADDING)/len;
  8.  
  9.      int pos = 0;
  10.  
  11.      for( int i=0; i<len; i++ ) {
  12.  
  13.           View c = getChildAt(i);
  14.  
  15.           c.layout( pos, 0, pos+w, b-t );
  16.  
  17.           pos += w + PADDING;
  18.  
  19.      }
  20.  
  21. }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
nexus7
Freshman
Freshman
 
Posts: 2
Joined: Wed Feb 10, 2010 2:29 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 12 guests