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 {android
public MyIndTitle(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
// TODO Auto-generated constructor stub
}canvas
public MyIndTitle(Context context) {
super(context);
// TODO Auto-generated constructor stub
}ide
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 stubspa
}
@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());get
}
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();//�����õ�ʵ�ʵĸ�
}
}}it
}io
@Override
protected void dispatchDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.dispatchDraw(canvas);
}class
}
import