自定义ViewGroup
package com.fenghongzhang.day011;
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class FengGroup extends ViewGroup {
int groupWidth,groupHeight;
private static final String TAG = "FengGroup";
public FengGroup(Context context) {
super(context);
}
public FengGroup(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
int mode = MeasureSpec.getMode(widthMeasureSpec);
int size = MeasureSpec.getSize(widthMeasureSpec);
switch (mode){
case MeasureSpec.EXACTLY:
Log.i(TAG, "onMeasure: ");
groupWidth = size;
break;
case MeasureSpec.AT_MOST:
Log.i(TAG, "onMeasure:111 ");
groupWidth = 50;
break;
}
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
switch (heightMode){
case MeasureSpec.EXACTLY:
Log.i(TAG, "onMeasure: ");
groupHeight = heightSize;
break;
case MeasureSpec.AT_MOST:
Log.i(TAG, "onMeasure:111 ");
groupHeight = 50;
break;
}
setMeasuredDimension(groupWidth,groupHeight);
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
int childCountWidth = 0;
int left = 0;
int childCount = getChildCount();
for (int i = 0; i < childCount; i++) {
View childAt = getChildAt(i);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) childAt.getLayoutParams();
childCountWidth += layoutParams.width;
Log.i(TAG, "onLayout: "+layoutParams.width);
Log.i(TAG, "onLayout: "+layoutParams.height);
int width = getWidth();
Log.i(TAG, "onLayout: "+width);
if(width > childCountWidth){
Log.i(TAG, "onLayout: 横排");
childAt.layout(left,0,layoutParams.width+left,layoutParams.height);
left += layoutParams.width;
}else{
Log.i(TAG, "onLayout: 竖排 ");
childAt.layout(0,layoutParams.height,layoutParams.width,layoutParams.height*2);
}
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
}
@Override
public LayoutParams generateLayoutParams(AttributeSet attrs) {
return new LinearLayout.LayoutParams(getContext(),attrs);
}
}
xml中的使用
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.fenghongzhang.day011.FengGroup
android:id="@+id/group"
android:layout_width="250dp"
android:background="@color/colorAccent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="100dp"
android:text="哈哈"
android:background="@color/colorPrimaryDark"
android:layout_height="100dp"></TextView>
<TextView
android:layout_width="100dp"
android:text="嘿嘿"
android:background="@color/colorPrimaryDark"
android:layout_height="100dp"></TextView>
</com.fenghongzhang.day011.FengGroup>
</LinearLayout>