阶梯布局
<com.bw.qgs.jieti.JieTi
android:background="@color/colorAccent"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
public class Jie extends ViewGroup { public Jie(Context context) { this(context,null); }
public Jie(Context context, AttributeSet attrs) {
this(context, attrs,-1);
}
public Jie(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
for (int i = 0;i<6;i++){
initText();
}
}
private TextView initText() {
TextView textView = new TextView(getContext());
textView.setBackgroundColor(Color.RED);
textView.setText("123");
textView.setTextSize(20);
textView.setTextColor(Color.BLACK);
this.addView(textView);
return textView;
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCount = getChildCount();
for (int i = 0;i<childCount;i++){
View childAt = getChildAt(i);
childAt.measure(0,0);
measureChild(childAt,childAt.getMeasuredWidth(),childAt.getMeasuredHeight());
int measuredWidth = childAt.getMeasuredWidth();
int measuredHeight = childAt.getMeasuredHeight();
childAt.layout(measuredWidth*i,measuredHeight*i,measuredWidth*(i+1),measuredHeight*(i+1));
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawColor(Color.YELLOW);
}
}