Java代码动态修改 ConstraintLayout 内控件布局的辅助类

上图

输入图片说明

ConstraintUtil.java

import android.support.annotation.IdRes;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.ConstraintSet;
import android.transition.TransitionManager;

/**
 * Created by xiaolei on 2017/9/8.
 */

public class ConstraintUtil
{
    private ConstraintLayout constraintLayout;
    private ConstraintBegin begin;
    private ConstraintSet applyConstraintSet = new ConstraintSet();
    private ConstraintSet resetConstraintSet = new ConstraintSet();

    public ConstraintUtil(ConstraintLayout constraintLayout)
    {
        this.constraintLayout = constraintLayout;
        resetConstraintSet.clone(constraintLayout);
    }

    /**
     * 开始修改
     * @return
     */
    public ConstraintBegin begin()
    {
        synchronized (ConstraintBegin.class)
        {
            if (begin == null)
            {
                begin = new ConstraintBegin();
            }
        }
        applyConstraintSet.clone(constraintLayout);
        return begin;
    }

    /**
     * 带动画的修改
     * @return
     */
    public ConstraintBegin beginWithAnim()
    {
        TransitionManager.beginDelayedTransition(constraintLayout);
        return begin();
    }

    /**
     * 重置
     */
    public void reSet()
    {
        resetConstraintSet.applyTo(constraintLayout);
    }

    /**
     * 带动画的重置
     */
    public void reSetWidthAnim()
    {
        TransitionManager.beginDelayedTransition(constraintLayout);
        resetConstraintSet.applyTo(constraintLayout);
    }
    
    public class ConstraintBegin
    {
        /**
         * 清除关系<br/>
         * 注意:这里不仅仅会清除关系,还会清除对应控件的宽高为 w:0,h:0
         * @param viewIds
         * @return
         */
        public ConstraintBegin clear(@IdRes int... viewIds)
        {
            for (int viewId : viewIds)
            {
                applyConstraintSet.clear(viewId);
            }
            return this;
        }

        /**
         * 清除某个控件的,某个关系
         * @param viewId
         * @param anchor
         * @return
         */
        public ConstraintBegin clear(int viewId,int anchor)
        {
            applyConstraintSet.clear(viewId,anchor);
            return this;
        }
        /**
         * 为某个控件设置 margin
         * @param viewId 某个控件ID
         * @param left marginLeft
         * @param top   marginTop
         * @param right marginRight
         * @param bottom marginBottom
         * @return
         */
        public ConstraintBegin setMargin(@IdRes int viewId, int left, int top, int right, int bottom)
        {
            setMarginLeft(viewId,left);
            setMarginTop(viewId,top);
            setMarginRight(viewId,right);
            setMarginBottom(viewId,bottom);
            return this;
        }

        /**
         * 为某个控件设置 marginLeft
         * @param viewId 某个控件ID
         * @param left marginLeft
         * @return
         */
        public ConstraintBegin setMarginLeft(@IdRes int viewId, int left)
        {
            applyConstraintSet.setMargin(viewId, ConstraintSet.LEFT, left);
            return this;
        }

        /**
         * 为某个控件设置 marginRight
         * @param viewId 某个控件ID
         * @param right marginRight
         * @return
         */
        public ConstraintBegin setMarginRight(@IdRes int viewId, int right)
        {
            applyConstraintSet.setMargin(viewId, ConstraintSet.RIGHT, right);
            return this;
        }

        /**
         * 为某个控件设置 marginTop
         * @param viewId 某个控件ID
         * @param top marginTop
         * @return
         */
        public ConstraintBegin setMarginTop(@IdRes int viewId, int top)
        {
            applyConstraintSet.setMargin(viewId, ConstraintSet.TOP, top);
            return this;
        }

        /**
         * 为某个控件设置marginBottom
         * @param viewId 某个控件ID
         * @param bottom marginBottom
         * @return
         */
        public ConstraintBegin setMarginBottom(@IdRes int viewId, int bottom)
        {
            applyConstraintSet.setMargin(viewId, ConstraintSet.BOTTOM, bottom);
            return this;
        }

        /**
         * 为某个控件设置关联关系 left_to_left_of
         * @param startId 
         * @param endId 
         * @return
         */
        public ConstraintBegin Left_toLeftOf(@IdRes int startId, @IdRes int endId)
        {
            applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.LEFT);
            return this;
        }

        /**
         * 为某个控件设置关联关系 left_to_right_of
         * @param startId
         * @param endId
         * @return
         */
        public ConstraintBegin Left_toRightOf(@IdRes int startId, @IdRes int endId)
        {
            applyConstraintSet.connect(startId, ConstraintSet.LEFT, endId, ConstraintSet.RIGHT);
            return this;
        }

        /**
         * 为某个控件设置关联关系 top_to_top_of
         * @param startId
         * @param endId
         * @return
         */
        public ConstraintBegin Top_toTopOf(@IdRes int startId, @IdRes int endId)
        {
            applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.TOP);
            return this;
        }

        /**
         * 为某个控件设置关联关系 top_to_bottom_of
         * @param startId
         * @param endId
         * @return
         */
        public ConstraintBegin Top_toBottomOf(@IdRes int startId, @IdRes int endId)
        {
            applyConstraintSet.connect(startId, ConstraintSet.TOP, endId, ConstraintSet.BOTTOM);
            return this;
        }
        /**
         * 为某个控件设置关联关系 right_to_left_of
         * @param startId
         * @param endId
         * @return
         */
        public ConstraintBegin Right_toLeftOf(@IdRes int startId, @IdRes int endId)
        {
            applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.LEFT);
            return this;
        }
        /**
         * 为某个控件设置关联关系 right_to_right_of
         * @param startId
         * @param endId
         * @return
         */
        public ConstraintBegin Right_toRightOf(@IdRes int startId, @IdRes int endId)
        {
            applyConstraintSet.connect(startId, ConstraintSet.RIGHT, endId, ConstraintSet.RIGHT);
            return this;
        }
        /**
         * 为某个控件设置关联关系 bottom_to_bottom_of
         * @param startId
         * @param endId
         * @return
         */
        public ConstraintBegin Bottom_toBottomOf(@IdRes int startId, @IdRes int endId)
        {
            applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.BOTTOM);
            return this;
        }

        /**
         * 为某个控件设置关联关系 bottom_to_top_of
         * @param startId
         * @param endId
         * @return
         */
        public ConstraintBegin Bottom_toTopOf(@IdRes int startId, @IdRes int endId)
        {
            applyConstraintSet.connect(startId, ConstraintSet.BOTTOM, endId, ConstraintSet.TOP);
            return this;
        }

        /**
         * 为某个控件设置宽度
         * @param viewId
         * @param width
         * @return
         */
        public ConstraintBegin setWidth(@IdRes int viewId, int width)
        {
            applyConstraintSet.constrainWidth(viewId, width);
            return this;
        }

        /**
         * 某个控件设置高度
         * @param viewId
         * @param height
         * @return
         */
        public ConstraintBegin setHeight(@IdRes int viewId, int height)
        {
            applyConstraintSet.constrainHeight(viewId, height);
            return this;
        }

        /**
         * 提交应用生效
         */
        public void commit()
        {
            applyConstraintSet.applyTo(constraintLayout);
        }
    }
}

使用

public void onApplyClick(View view)
    {
        constraintUtil = new ConstraintUtil(constraintLayout);
        ConstraintUtil.ConstraintBegin begin = constraintUtil.beginWithAnim();
        
        begin.clear(R.id.button1,R.id.button2,R.id.button3);
        
        begin.Left_toLeftOf(R.id.button1,R.id.main);
        begin.Left_toRightOf(R.id.button2,R.id.button1);
        begin.Right_toLeftOf(R.id.button2,R.id.button3);
        begin.Right_toRightOf(R.id.button3,R.id.main);
        
        begin.setWidth(R.id.button1, ConstraintSet.WRAP_CONTENT);
        begin.setWidth(R.id.button2, ConstraintSet.WRAP_CONTENT);
        begin.setWidth(R.id.button3, ConstraintSet.WRAP_CONTENT);
        begin.setHeight(R.id.button1, ConstraintSet.WRAP_CONTENT);
        begin.setHeight(R.id.button2, ConstraintSet.WRAP_CONTENT);
        begin.setHeight(R.id.button3, ConstraintSet.WRAP_CONTENT);
        
        begin.commit();
    }

本文参与腾讯云自媒体分享计划,欢迎正在阅读的你也加入,一起分享。

发表于

我来说两句

0 条评论
登录 后参与评论

相关文章

来自专栏清墨_iOS分享

自学Swift之路(一)UI入手之基本控件

本系列文章都是以有OC基础来写的,所以注释不会写太多,对基础不够了解的同学可能不太适合,另外本系列文章不是以教程式的形式来写,是记录学习过程的,所以不会有多少讲...

58511
来自专栏everhad

[虾扯蛋] android界面框架-Window

从纯sdk及framwork的角度看,android中界面框架相关的类型有:Window,WindowManager,View等。下面就以这几个类为出发点来概览...

1888
来自专栏中国Android研究院

自定义控件之带渐变色的圆形进度条

七夕到了,祝大家七夕节快乐! 今天给大家讲解一个简单的进度条的自定义。首页呢,我们看下效果图:

1302
来自专栏Android Note

Android – 自定义Loading圆点

1334
来自专栏Android中高级开发

Android开发之漫漫长途 Ⅳ——Activity的显示之ViewRootImpl初探

该文章是一个系列文章,是本人在Android开发的漫漫长途上的一点感想和记录,我会尽量按照先易后难的顺序进行编写该系列。该系列引用了《Android开发艺术探索...

793
来自专栏青蛙要fly的专栏

Android技能树 — PopupWindow小结

关于下拉选择框,估计大家都有很多选择,我在以前的文章:项目需求讨论-HyBrid模式需求改造 上写过下拉框选择这一块,正好用的Spinner。

491
来自专栏Android Note

Android – 环形进度条

1035
来自专栏Sorrower的专栏

界面无小事(七):使用代码动态增删布局

593
来自专栏向治洪

android沉浸式状态栏的实现

在style.xml中添加 <style name="Theme.Timetodo" parent="@android:style/Theme.Holo.L...

1957
来自专栏Sorrower的专栏

使用代码动态增删布局(2018.8重编版)

502

扫码关注云+社区