android自定义view viewgroup

1xml布局

<?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.example.viewgroup.ViewGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="#44ff44"
        app:ori="all"
       >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="峨眉月岛"
            android:textSize="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="峨眉月岛"
            android:textSize="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="峨眉月岛"
            android:textSize="20dp" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="峨眉月岛"
            android:textSize="20dp" />
    </com.example.viewgroup.ViewGroup>


</LinearLayout>

2values下创建attrs属性

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="ViewGroup">
        <attr name="ori" format="enum">
            <enum name="horizontal" value="0"/>
            <enum name="vertical" value="1"/>
            <enum name="all" value="2"/>
        </attr>
    </declare-styleable>
</resources>

3新建一个activity

package com.example.viewgroup;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
public class ViewGroup extends android.view.ViewGroup {

    private int op;
    int marginleft = 48;
    int margintop = 48;

    public ViewGroup(Context context) {
        this(context,null);
    }

    public ViewGroup(Context context, AttributeSet attrs) {
        this(context, attrs,0);
    }

    public ViewGroup(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context,attrs);
    }

    private void initView(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ViewGroup);
        op = typedArray.getInt(R.styleable.ViewGroup_ori, 0);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec,heightMeasureSpec);
        int width=0;
        int height=0;
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        if (op==0){
            switch (widthMode){
                case MeasureSpec.EXACTLY:
                    width=widthSize;
                    height=heightSize;
                    break;
                case MeasureSpec.AT_MOST:
                    for (int i=0;i<getChildCount();i++){
                        View childAt = getChildAt(i);
                        int measuredWidth = childAt.getMeasuredWidth();
                        int measuredHeight = childAt.getMeasuredHeight();
                        width=width+measuredWidth+marginleft;
                        height=measuredHeight;
                    }
                    break;
                case MeasureSpec.UNSPECIFIED:
                    break;
            }
        }else if (op==1){
            switch (heightMode){
                case MeasureSpec.EXACTLY:
                    width=widthSize;
                    height=heightSize;
                    break;
                case MeasureSpec.AT_MOST:
                    for (int i=0;i<getChildCount();i++){
                        View childAt = getChildAt(i);
                        int measuredWidth = childAt.getMeasuredWidth();
                        int measuredHeight = childAt.getMeasuredHeight();
                        height=height+measuredHeight+margintop;
                        width=measuredWidth;
                    }
                    break;
                case MeasureSpec.UNSPECIFIED:
                    break;
            }
        }else if (op==2){
            switch (heightMode){
                case MeasureSpec.EXACTLY:
                    width=widthSize;
                    height=heightSize;
                    break;
                case MeasureSpec.AT_MOST:
                    for (int i=0;i<getChildCount();i++){
                        View childAt = getChildAt(i);
                        int measuredWidth = childAt.getMeasuredWidth();
                        int measuredHeight = childAt.getMeasuredHeight();
                        height=height+measuredHeight;
                        width=measuredWidth+width;
                    }
                    break;
                case MeasureSpec.UNSPECIFIED:
                    break;
            }
        }
        setMeasuredDimension(width,height);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width=0;
        int top=0;
        for (int i=0;i<getChildCount();i++){
            View childAt = getChildAt(i);
            int measuredHeight = childAt.getMeasuredHeight();
            int measuredWidth = childAt.getMeasuredWidth();
            if (op==0){
                childAt.layout(width,top,width+measuredWidth,top+measuredHeight);
                width=width+measuredWidth+marginleft;
            }else if (op==1){
                childAt.layout(width,top,width+measuredWidth,top+measuredHeight);
                top=top+measuredHeight+margintop;
            }else if (op==2){
                childAt.layout(width,top,width+measuredWidth,top+measuredHeight);
                width=width+measuredWidth;
                top=top+measuredHeight;
            }
        }
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
    }
}

效果图在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_44258719/article/details/87877898