自定义ViewGroup

1、闲聊

        今天给大家带来一篇官方的自定义ViewGroup的教程,说白了,就是教大家如何自定义ViewGroup,如果你对自定义ViewGroup还不是很了解,或者正想学习如何自定义,那么你可以好好看看这篇博客 或者 去看官方api 》》》

http://android.xsoftlab.net/reference/android/view/ViewGroup.html

2、简介

        ViewGroup是一个可以包含其他视图(称为子节点)的特殊视图。视图组是布局和视图容器的基类。该类还定义了 ViewGroup.LayoutParams用作布局参数的基类的类。

3、例子

        这是官方的一个例子:

public class CustomLayout extends ViewGroup {

    /**
     * 左沟里儿童使用的空间。
     */
    private int mLeftWidth;
    /**
     * 孩子们在右边的排水沟里所使用的空间。
     */
    private int mRightWidth;
    /**
     * 这些都是基于它们的重力来计算儿童帧的。
     */
    private final Rect mTmpContainerRect = new Rect();
    private final Rect mTmpChildRect = new Rect();

    public CustomLayout(Context context) {
        super(context);
    }

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

    public CustomLayout(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }

    /**
     * 任何不滚动的布局管理器都需要这个。
     */
    @Override
    public boolean shouldDelayChildPressedState() {
        return false;
    }

    /**
     * 要求所有的孩子测量自己,并根据孩子的情况来计算这个布局的尺寸。
     */
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        int count = getChildCount();
        // 这些都记录了我们在左边和右边所使用的空间的位置;我们需要成员变量,以便以后可以使用这些变量。
        mLeftWidth = 0;
        mRightWidth = 0;
        // 度量最终将计算这些值。
        int maxHeight = 0;
        int maxWidth = 0;
        int childState = 0;
        // 遍历所有的孩子,测量他们,并根据他们的尺寸计算我们的尺寸。
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                // 测量的孩子。
                measureChildWithMargins(child, widthMeasureSpec, 0, heightMeasureSpec, 0);
                // 根据布局参数更新我们的大小信息。那些被要求放在左边或右边的孩子会被放在那些排水沟里。
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                if (lp.position == LayoutParams.POSITION_LEFT) {
                    mLeftWidth += Math.max(maxWidth,
                            child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
                } else if (lp.position == LayoutParams.POSITION_RIGHT) {
                    mRightWidth += Math.max(maxWidth,
                            child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
                } else {
                    maxWidth = Math.max(maxWidth,
                            child.getMeasuredWidth() + lp.leftMargin + lp.rightMargin);
                }
                maxHeight = Math.max(maxHeight,
                        child.getMeasuredHeight() + lp.topMargin + lp.bottomMargin);
                childState = combineMeasuredStates(childState, child.getMeasuredState());
            }
        }
        // 总宽度是所有内子的最大宽度加上排水沟的宽度。
        maxWidth += mLeftWidth + mRightWidth;
        // 检查我们的最小高度和宽度
        maxHeight = Math.max(maxHeight, getSuggestedMinimumHeight());
        maxWidth = Math.max(maxWidth, getSuggestedMinimumWidth());
        // 我们最终的尺寸报告。
        setMeasuredDimension(resolveSizeAndState(maxWidth, widthMeasureSpec, childState),
                resolveSizeAndState(maxHeight, heightMeasureSpec,
                        childState << MEASURED_HEIGHT_STATE_SHIFT));
    }

    /**
     * 在这个布局中放置所有的孩子。
     */
    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        final int count = getChildCount();
        // 这些是我们正在进行布局的左和右边缘。
        int leftPos = getPaddingLeft();
        int rightPos = right - left - getPaddingRight();
        // 这是阴沟里的中间区域。
        final int middleLeft = leftPos + mLeftWidth;
        final int middleRight = rightPos - mRightWidth;
        // 这些是我们执行布局的顶部和底部边缘。
        final int parentTop = getPaddingTop();
        final int parentBottom = bottom - top - getPaddingBottom();
        for (int i = 0; i < count; i++) {
            final View child = getChildAt(i);
            if (child.getVisibility() != GONE) {
                final LayoutParams lp = (LayoutParams) child.getLayoutParams();
                final int width = child.getMeasuredWidth();
                final int height = child.getMeasuredHeight();
                // 计算我们放置这个子的坐标系。
                if (lp.position == LayoutParams.POSITION_LEFT) {
                    mTmpContainerRect.left = leftPos + lp.leftMargin;
                    mTmpContainerRect.right = leftPos + width + lp.rightMargin;
                    leftPos = mTmpContainerRect.right;
                } else if (lp.position == LayoutParams.POSITION_RIGHT) {
                    mTmpContainerRect.right = rightPos - lp.rightMargin;
                    mTmpContainerRect.left = rightPos - width - lp.leftMargin;
                    rightPos = mTmpContainerRect.left;
                } else {
                    mTmpContainerRect.left = middleLeft + lp.leftMargin;
                    mTmpContainerRect.right = middleRight - lp.rightMargin;
                }
                mTmpContainerRect.top = parentTop + lp.topMargin;
                mTmpContainerRect.bottom = parentBottom - lp.bottomMargin;
                // 使用孩子的重力和大小来决定其容器内的最终框架。
                Gravity.apply(lp.gravity, width, height, mTmpContainerRect, mTmpChildRect);
                // 孩子的地方。
                child.layout(mTmpChildRect.left, mTmpChildRect.top,
                        mTmpChildRect.right, mTmpChildRect.bottom);
            }
        }
    }

    //实现的其余部分用于自定义的每个子布局参数。
    //如果您不需要这些(例如,您正在编写一个布局管理器
    //那对孩子的定位是固定的),你可以把这一切都放下来。
    @Override
    public LayoutParams generateLayoutParams(AttributeSet attrs) {
        return new CustomLayout.LayoutParams(getContext(), attrs);
    }

    @Override
    protected LayoutParams generateDefaultLayoutParams() {
        return new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
    }

    @Override
    protected ViewGroup.LayoutParams generateLayoutParams(ViewGroup.LayoutParams p) {
        return new LayoutParams(p);
    }

    @Override
    protected boolean checkLayoutParams(ViewGroup.LayoutParams p) {
        return p instanceof LayoutParams;
    }

    /**
     * 自定义每个孩子布局信息。
     */
    public static class LayoutParams extends MarginLayoutParams {
        /**
         * 应用于这些布局参数的视图的重力。
         */
        public int gravity = Gravity.TOP | Gravity.START;
        public static final int POSITION_MIDDLE = 0;
        public static final int POSITION_LEFT = 1;
        public static final int POSITION_RIGHT = 2;
        public int position = POSITION_MIDDLE;

        public LayoutParams(Context c, AttributeSet attrs) {
            super(c, attrs);
            //在通货膨胀期间,从布局XML中拉出布局的param值。如果您不关心改变XML中的布局行为,那么这是不需要的。
            TypedArray a = c.obtainStyledAttributes(attrs, R.styleable.CustomLayout);
            gravity = a.getInt(R.styleable.CustomLayout_android_layout_gravity, gravity);
            position = a.getInt(R.styleable.CustomLayout_layout_position, position);
            a.recycle();
        }

        public LayoutParams(int width, int height) {
            super(width, height);
        }

        public LayoutParams(ViewGroup.LayoutParams source) {
            super(source);
        }
    }
}

自定属性:
<declare-styleable name="CustomLayout">
    <attr name="android:layout_gravity" />
    <attr name="layout_position">
        <enum name="middle" value="0" />
        <enum name="left" value="1" />
        <enum name="right" value="2" />
    </attr>
</declare-styleable>

4、使用

<com.taoyong.widget.group.CustomLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/delinputview"
        android:gravity="center"
        android:text="Test测试 Left"
        android:textColor="#000" />

    <TextView
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@drawable/delinputview"
        android:gravity="center"
        android:text="Test测试 Center"
        android:textColor="#000"
        app:layout_position="right" />

</com.taoyong.widget.group.CustomLayout>

5、效果图

这里写图片描述

6、资源

demo地址:

https://github.com/fountaintao/CustomControl/blob/master/widget/src/main/java/com/taoyong/widget/group/CustomLayout.java


官方地址:

http://android.xsoftlab.net/reference/android/view/ViewGroup.html



官方虽然很多东西没有解释不全,但是还是的多看,总会找到你所想的!

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值