博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Java代码动态修改 ConstraintLayout 内控件布局的辅助类
阅读量:6555 次
发布时间:2019-06-24

本文共 8409 字,大约阅读时间需要 28 分钟。

hot3.png

##上图

输入图片说明

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    {        /**         * 清除关系
* 注意:这里不仅仅会清除关系,还会清除对应控件的宽高为 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();    }

转载于:https://my.oschina.net/xiaolei123/blog/1531977

你可能感兴趣的文章
tableView 有点击效果 但是不让选中的方法
查看>>
onclick事件与onserverclick事件
查看>>
C#winform中DataGridView常用的属性
查看>>
iOS绘制线条的使用
查看>>
检索 COM 类工厂中 CLSID 为 {00024500-0000-0000-C000-000000000046} 的组件时失败,原因是出现以下错误: 80070005。...
查看>>
MPLAB X IDE 中文注释乱码问题解决.
查看>>
学习笔记——决策树
查看>>
关于HTML标签中的一些容易忘记常用样式属性
查看>>
spring session实现集群中session共享
查看>>
一些职场的忠告
查看>>
oracle权限详解
查看>>
Highcharts制作图片表设置线条颜色和粗细
查看>>
iOS开发多线程篇—线程间的通信
查看>>
【转】Office 2003 EXCEL多窗口打开
查看>>
代码实现导航栏分割线
查看>>
由ibatis向mybatis的转变
查看>>
Android Button事件
查看>>
IO流(PrintWriter) 很重要的一个类 核心
查看>>
python实现WordCount基础和拓展功能
查看>>
《开拓者研发团队》——基于弹幕评论的大数据分析平台项目的原型设计与开发...
查看>>