Viewgroup(View容器)

来源:互联网 发布:windows ant下载 编辑:程序博客网 时间:2024/06/05 22:55
容器内的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();        }    }}



原创粉丝点击