美文网首页
自定义ViewGroup(一)

自定义ViewGroup(一)

作者: cao苗子 | 来源:发表于2019-08-29 09:56 被阅读0次

1.先看效果

image.png

2.效果执行代码


/**
 * created by panshimu
 * on 2019/8/28
 */
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);
    }

    /**
     * 摆放位置
     * @param changed
     * @param l
     * @param t
     * @param r
     * @param b
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int childCount = getChildCount();

        int left = 0,top = 0,right = 0,bottom = 0;

        for (int i = 0; i < childCount; i++) {

            View childView = getChildAt(i);

            right = childView.getMeasuredWidth();

            bottom = childView.getMeasuredHeight() + top;

            //摆放子view的位置
            childView.layout(left,top,right,bottom);

            top += childView.getMeasuredHeight();

        }
    }

    /**
     * 测量宽高
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            //测量子view的宽高
            measureChild(childView,widthMeasureSpec,heightMeasureSpec);
        }
        //测量自己的宽高
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    }


    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new MarginLayoutParams(getContext(),attrs);
    }
}

资源文件代码:

<?xml version="1.0" encoding="utf-8"?>
<com.miaozi.myviewgroup.MyViewGroup 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary"
    tools:context=".MainActivity">
    <Button
        android:text="第一"
        android:layout_width="match_parent"
        android:layout_height="100dp" />
    <Button
        android:text="第一"
        android:layout_width="200dp"
        android:layout_height="100dp" />
    <Button
        android:text="第一"
        android:layout_width="100dp"
        android:layout_height="100dp" />
    <TextView
        android:text="我的妈妈呀"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</com.miaozi.myviewgroup.MyViewGroup>

主要看两个点:
一个是onMeasure,另一个就是 onLayout()。

onMeasure()

测量控件的宽高包括自view的宽高

 /**
     * 测量宽高
     * @param widthMeasureSpec
     * @param heightMeasureSpec
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childView = getChildAt(i);
            //测量子view的宽高
            measureChild(childView,widthMeasureSpec,heightMeasureSpec);
        }
        //测量自己的宽高
        super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    }

onLayout view的摆放

 /**
     * 摆放位置
     * @param changed
     * @param l
     * @param t
     * @param r
     * @param b
     */
    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {

        int childCount = getChildCount();

        int left = 0,top = 0,right = 0,bottom = 0;

        for (int i = 0; i < childCount; i++) {

            View childView = getChildAt(i);

            right = childView.getMeasuredWidth();

            bottom = childView.getMeasuredHeight() + top;

            //摆放子view的位置
            childView.layout(left,top,right,bottom);

            top += childView.getMeasuredHeight();

        }
    }

好了,就到这里了。大家只需要关注 onMeasure和onLayout就行,一个是测量一个是摆放。
注意的点还有一个就是:MyViewGroup的大小设置的都是

  android:layout_width="match_parent"
  android:layout_height="match_parent"

如果我们设置的是

 android:layout_width="wrap_content"
 android:layout_height="wrap_content"

大家可以自己看一下效果

相关文章

网友评论

      本文标题:自定义ViewGroup(一)

      本文链接:https://www.haomeiwen.com/subject/hipbectx.html