AndroidカスタムView Group

9671 ワード

package com.mapbar.adas.camera;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by sunxx on 2018/2/27.
 */

public class MyViewGroup extends ViewGroup {
    private String TAG = "CAM";

    public MyViewGroup(Context context) {
        super(context);
    }

    public MyViewGroup(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //   MeasureSpec
        //MeasureSpec.getMode(MeasureSpec.AT_MOST);
        int count = getChildCount();
        //measureChildren        view
        //measureChildren(widthMeasureSpec, heightMeasureSpec);
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
            //      view
            measureChild(child, widthMeasureSpec, heightMeasureSpec);
        }

    }

    /**
     * @param changed
     * @param l
     * @param t
     * @param r
     * @param b
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        Log.i(TAG, "  " + getChildCount());
        //     view            
        //    4 view      4   
        int count = getChildCount();
        int height = 0;
        for (int i = 0; i < count; i++) {
            View child = getChildAt(i);
//            child.layout(0, height, child.getMeasuredWidth(), child.getMeasuredHeight()+height);
//            height+=child.getHeight();
            switch (i) {
                case 0:
                    child.layout(0, 0, child.getMeasuredWidth(), child.getMeasuredHeight());
                    break;
                case 1:
                    child.layout(getWidth() - child.getMeasuredWidth(), 0, getWidth(), child.getMeasuredHeight());
                    break;
                case 2:
                    child.layout(0, getHeight() - child.getMeasuredHeight(), child.getMeasuredWidth(), getHeight());
                    break;
                case 3:
                    child.layout(getWidth() - child.getMeasuredWidth(), getHeight() - child.getMeasuredHeight(), getWidth(), getHeight());
                    break;
            }
        }
    }

    /**
     * @deprecated {@link #MyViewGroup(Context)}
     */
    public void init() {

    }

    /**
     * @param a
     * @see #init()   init    
     */
    public void newinit(int a) {

    }
}