Viewgroup(View容器)

容器内的view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    <com.example.fanjie.mockone.TitleView
        android:id="@+id/ttv_title"
        android:layout_width="match_parent"
        android:layout_height="35dp">
    </com.example.fanjie.mockone.TitleView>
    <com.example.fanjie.mockone.MyViewGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <TextView
            android:layout_width="100dp"
            android:background="#0000ff"
            android:layout_height="50dp" />
        <TextView
            android:layout_width="100dp"
            android:background="#17b293"
            android:layout_height="50dp" />
        <TextView
            android:layout_width="100dp"
            android:background="#b2eb09"
            android:layout_height="50dp" />
    </com.example.fanjie.mockone.MyViewGroup>
</LinearLayout>
创建MyViewGroup
import android.content.Context;
import android.graphics.Canvas;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;

/**
 * author:Created by WangZhiQiang on 2017/11/3.
 */

public class MyViewGroup extends ViewGroup {
    public MyViewGroup(Context context) {
        super(context);
    }

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

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

    //一般情况下不会再ViewGroup使用onDraw()方法,ViewGroup主要承担的是盛放View的职责
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
//        Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
//        paint.setColor(Color.RED);
//        canvas.drawCircle(50,50,50,paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //测量View的宽高,只有ViewGroup中有这个方法,直接继承自View是没有这个方法的
        measureChildren(widthMeasureSpec, heightMeasureSpec);
//        //ViewGroup自己的大小
//        setMeasuredDimension(DensityUtil.dip2px(getContext(),200),DensityUtil.dip2px(getContext(),200));

    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        //拿到子控件的个数
        int childCount = getChildCount();
        //定义一个临时的高度变量
        int startHeight=0;
        int startWidth=0;
        //循环遍历每一个View
        for(int i=0;i<childCount;i++){
            View v = getChildAt(i);
            //给每一个View设置自己的位置
            //v.getMeasuredHeight()自身的高度,
            //v.getMeasuredWidth()自身宽度
            v.layout(startWidth,startHeight,startWidth+v.getMeasuredWidth(),startHeight+v.getMeasuredHeight());
            startHeight+=v.getMeasuredHeight();
            startWidth+=v.getMeasuredWidth();
        }
    }

}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值