自定义view

package com.example.zh.day08;

import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

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

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

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

private void inten() {

}
public int oritation = 2;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    //获取子view的宽高
    measureChildren(widthMeasureSpec, heightMeasureSpec);

    int heighMeasured = 0;
    int widthMeasured = 0;
    //拿到view的高使用的模式模式
    int heightmode = MeasureSpec.getMode(heightMeasureSpec);
    //拿到view的高的大小
    int heightsize = MeasureSpec.getSize(heightMeasureSpec);
    //拿到view的宽的大小
    int widthsize = MeasureSpec.getSize(widthMeasureSpec);
    //拿到view的宽使用的模式模式
    int widthmode = MeasureSpec.getMode(widthMeasureSpec);
    switch (widthmode) {
        //写固定的长度大小边距模式dp
        case MeasureSpec.EXACTLY:
            //设置view宽高
            widthMeasured = widthsize;
            heighMeasured = heightsize;
            break;
        //写wrap_content时调用的模式
        case MeasureSpec.AT_MOST:
            if (oritation == 1) {
                int measuredHeight = 0;
                for (int i = 0; i < getChildCount(); i++) {
                    View childAt = getChildAt(i);
                    measuredHeight = childAt.getMeasuredHeight();
                    int measuredWidth = childAt.getMeasuredWidth();
                    widthMeasured += measuredWidth + 48;

                }
                heighMeasured += measuredHeight + 48;
            } else if (oritation == 0) {
                int measuredWidth = 0;
                for (int i = 0; i < getChildCount(); i++) {
                    View childAt = getChildAt(i);
                    int measuredHeight = childAt.getMeasuredHeight();
                    measuredWidth = childAt.getMeasuredWidth();
                    heighMeasured += measuredHeight + 48;
                }
                widthMeasured += measuredWidth + 48;
            } else if (oritation==2) {
                for (int i = 0; i <getChildCount() ; i++) {
                    View childAt = getChildAt(i);
                    int measuredHeight = childAt.getMeasuredHeight();
                    int measuredWidth = childAt.getMeasuredWidth();
                    heighMeasured+=measuredHeight+48;
                    widthMeasured+=measuredWidth+48;
                }
            }

            break;
        //未定义时的模式
        case MeasureSpec.UNSPECIFIED:
            break;
    }
    setMeasuredDimension(widthMeasured, heighMeasured);
}



@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
    //定义一个变量为子view到父view的上间距
    int topall = 0;
    //定义一个变量为子view到父view的左间距
    int leftall = 0;
    //定义一个变量为子view间的左间隔
    int marginLeft = 40;
    //定义一个变量为子view间的上间隔
    int marginTop = 40;
    //循环view里的子wiew,getChildCount子view的数量
    for (int i = 0; i < getChildCount(); i++) {
        //获取到子view
        View childAt = getChildAt(i);
        //子view的宽
        int width = childAt.getMeasuredWidth();
        //子view的高
        int height = childAt.getMeasuredHeight();
        if (oritation == 0) {
            //设置子view的位置
            childAt.layout(0, topall, 0 + width, topall + height);
            topall += height + marginTop;

        } else if (oritation == 1) {
            childAt.layout(leftall, marginTop, leftall + width, marginTop + height);
            leftall += width + marginLeft;
        } else if (oritation==2){
            childAt.layout(leftall, topall, leftall + width,  height+topall);
            leftall += width + marginLeft;
            topall += height + marginTop;
        }

    }

}

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器学习模型机器
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值