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 条评论
登录 后参与评论

相关文章

来自专栏linux驱动个人学习

高通Audio中ASOC的machine驱动

ASoC被分为Machine、Platform和Codec三大部分,其中的Machine驱动负责Platform和Codec之间的耦合以及部分和设备或板子特定的...

9774
来自专栏MelonTeam专栏

Bitmap 源码阅读笔记

导语: Android 系统上的图片的处理,跟Bitmap 这个类脱不了关系,我们有必要去深入阅读里面的源码,以便在工作中能更好的处理Bitmap相关的问题...

2498
来自专栏WOLFRAM

错觉艺术的巅峰,错觉图形大师M.C. Escher的不可能方块的可能模型

1343
来自专栏前端儿

Web 前端颜色值--字体--使用,整理整理

颜色值 CSS 颜色使用组合了红绿蓝颜色值 (RGB) 的十六进制 (hex) 表示法进行定义。对光源进行设置的最低值可以是 0(十六进制 00)。最高值是 2...

2312
来自专栏祝威廉

Spark Streaming Dynamic Resource Allocation

DRA has already been implemented since Spark 1.2 . However the existing Spark DR...

1403
来自专栏一个会写诗的程序员的博客

java.base.jmod

/Library/Java/JavaVirtualMachines/jdk-9.jdk/Contents/Home/jmods$ jmod list java....

1112
来自专栏搞前端的李蚊子

Html5模拟通讯录人员排序(sen.js)

// JavaScript Document  var PY_Json_Str = ""; var PY_Str_1 = ""; var PY_Str_...

5906
来自专栏我和未来有约会

简练的视图模型 ViewModel

patterns & practices Developer Center 发布了 Unity Application Block 1.2 for Silver...

2179
来自专栏Netkiller

Hyperledger Fabric Chaincode 开发

中国广东省深圳市龙华新区民治街道溪山美地 518131 +86 13113668890 <netkiller@msn.com>

61511
来自专栏IT杂记

Storm客户端提交任务失败原因分析

2550

扫码关注云+社区