标签:
package com.example.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.LinearLayout;
public class MyIndTitle extends LinearLayout {
 public MyIndTitle(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  // TODO Auto-generated constructor stub
 }
 public MyIndTitle(Context context) {
  super(context);
  // TODO Auto-generated constructor stub
 }
 private int everyMargin=20;//??????????20.
 private int topMargin=20;//????20??
 public int allWith;
 private int allWidth;
 private int allHeight;
 public MyIndTitle(Context context, AttributeSet attrs) {
  super(context, attrs);
  // TODO Auto-generated constructor stub
 }
 @Override
 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
  // TODO Auto-generated method stub
  super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  int childCount = getChildCount();
  allWidth = getMeasuredWidth();
  for (int i = 0; i <childCount; i++) {
   View view = getChildAt(i);
   view.measure(MeasureSpec.UNSPECIFIED,MeasureSpec.UNSPECIFIED);
  }
  setMeasuredDimension(allWidth,allHeight);
 }
 @Override
 protected void onLayout(boolean changed, int l, int t, int r, int b) {
  // TODO Auto-generated method stub
  super.onLayout(changed, l, t, r, b);
  Log.e("para",allWidth+"");
  for (int i = 0; i <getChildCount(); i++) {
   View childView = getChildAt(i);
   if(i==0){
    topMargin=20;//???????20 ?????ж???
    childView.layout(everyMargin,topMargin,everyMargin+childView.getMeasuredWidth(),topMargin+childView.getMeasuredHeight());
   }
   else {
    View prechildView = getChildAt(i-1);
    int x=prechildView.getRight()+everyMargin;
    if(x+childView.getMeasuredWidth()+everyMargin<=allWidth){
     childView.layout(x, topMargin,x+childView.getMeasuredWidth(),topMargin+childView.getMeasuredHeight());
    }
    else {
     childView.layout(everyMargin,prechildView.getBottom()+20,everyMargin+childView.getMeasuredWidth(),prechildView.getBottom()+20+childView.getMeasuredHeight());
     topMargin=prechildView.getBottom()+20;
     allHeight=prechildView.getBottom()+20+childView.getMeasuredHeight();//????????????
    }
   }}
}
 @Override
 protected void dispatchDraw(Canvas canvas) {
  // TODO Auto-generated method stub
  super.dispatchDraw(canvas);
 }
}
标签:
原文地址:http://my.oschina.net/u/2334470/blog/519052