java把控件跑挂了_Java代码动态修改 ConstraintLayout 内控件布局的辅助类

##上图

0818b9ca8b590ca3270a3433284dd417.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();

}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/327753.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

json vs obj

var obj {a: Hello, b: World}; //这是一个对象,注意键名也是可以使用引号包裹的 var json {"a": "Hello", "b": "World"}; //这是一个 JSON 字符串,本质是一个字符串在 JavaScript 语言中,一切都…

Vue动态组件

转载自 Vue动态组件 在动态组件上使用 keep-alive 我们之前曾经在一个多标签的界面中使用 is 特性来切换不同的组件&#xff1a; <component v-bind:is"currentTabComponent"></component> 当在这些组件之间切换的时候&#xff0c;你有时会想保持这些组…

ASP.NET Core Kestrel部署HTTPS

ASP.NET Core配置 Kestrel部署HTTPS。现在大部分网站已经部署HTTPS&#xff0c;大家对于安全越来越重视。 今天简单介绍一下ASP.NET Core 部署HTTPS&#xff0c;直接通过配置Kestrel。大家也可以通过前置Nginx来部署HTTPS。 下面直接进入正题。 新建项目并添加引用 新建一个ASP…

Mybatis+mysql动态分页查询数据案例——分页工具类(Page.java)

package cn.bdqn.mhouse.util;import java.util.ArrayList; import java.util.List;import cn.bdqn.mhouse.entity.House;/*** * * 项目名称&#xff1a;mhouse * 类名称&#xff1a;Page * 类描述&#xff1a; 分页的工具类 * 创建人&#xff1a;Mu Xiongxiong …

JSON 和 JavaScript 对象互转

JSON 和 JavaScript 对象互转 要实现从JSON字符串转换为JavaScript 对象&#xff0c;使用 JSON.parse() 方法&#xff1a; var obj JSON.parse({"a": "Hello", "b": "World"}); //结果是 {a: Hello, b: World}要实现从JavaScript 对…

java 时钟 算法分析_java实现时钟方法汇总

import java.awt.Dimension;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Timer;import java.util.TimerTask;import javax.swing.JFrame;import javax.swing.JLabel;import javax.swing.JPanel;//第一种比较推荐&…

Java IO: 网络

转载自 Java IO: 网络译文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197gmail.com) 校对&#xff1a;方腾飞 Java中网络的内容或多或少的超出了Java IO的范畴。关于Java网络更多的是在我的Java网络教程中探讨。但是既然网络是一个常见的数据来源以及数据流目的地&#xf…

配置phython环境

参考资料 https://www.runoob.com/python/python-install.html https://www.cnblogs.com/huangbiquan/p/7784533.html Python下载 Python最新源码&#xff0c;二进制文档&#xff0c;新闻资讯等可以在Python的官网查看到&#xff1a; Python官网&#xff1a;https://www.py…

ASP.NET Core 之 Identity 入门(三)

前言 在上一篇文章中&#xff0c;我们学习了 CookieAuthentication 中间件&#xff0c;本篇的话主要看一下 Identity 本身。 最早2005年 ASP.NET 2.0 的时候开始&#xff0c; Web 应用程序在处理身份验证和授权有了很多的变化&#xff0c;多了比如手机端&#xff0c;平板等&…

玩物得志Java笔试题_代码规范利器-CheckStyle

本期内容分为五个部分&#xff0c;阅读时长预估7分钟&#xff1a;使用背景CheckStyle使用意义CheckStyle安装与使用CheckStyle检查配置示例落地使用情况及效果使用背景玩物得志目前还处在一个狂奔业务的时期&#xff0c;开发一般都全力支撑业务的快速奔跑&#xff0c;没有太多的…

Json交互处理

Json交互处理 JSON简介 JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式&#xff0c;目前使用特别广泛。采用完全独立于编程语言的文本格式来存储和表示数据。简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。易于人阅读和编写&#xff0…

基于Spring boot,使用idea方便地切换启动环境

https://blog.csdn.net/mate_ge/article/details/78624579 基于Spring boot&#xff0c;使用idea方便地切换启动环境 原创martsforever 最后发布于2017-11-24 14:49:30 阅读数 17615 收藏 展开 在真实项目开发的时候&#xff0c;一定会有多个环境&#xff0c;这里以开发环境和…

Mybatis+mysql动态分页查询数据案例——条件类(HouseCondition)

package cn.bdqn.mhouse.entity; /*** * * 项目名称&#xff1a;house * 类名称&#xff1a;HouseCondition * 类描述&#xff1a; 动态查询房屋信息的条件类 * 创建人&#xff1a;Mu Xiongxiong * 创建时间&#xff1a;2017-3-10 下午9:39:21 * 修改人&…

Java IO: 字节和字符数组

转载自 Java IO: 字节和字符数组译文链接 作者&#xff1a; Jakob Jenkov 译者&#xff1a;homesick 内容列表 从InputStream或者Reader中读入数组从OutputStream或者Writer中写数组 在java中常用字节和字符数组在应用中临时存储数据。而这些数组又是通常的数据读取来源或…

利用 async amp; await 的异步编程

一、异步编程的简介 通过使用异步编程&#xff0c;你可以避免性能瓶颈并增强应用程序的总体响应能力。 Visual Studio 2012 引入了一个简化的方法&#xff0c;异步编程&#xff0c;在 .NET Framework 4.5 和 Windows 运行时利用异步支持。编译器可执行开发人员曾进行的高难度工…

1分钟学会python_快速入门:十分钟学会Python

类Python支持有限的多继承形式。私有变量和方法可以通过添加至少两个前导下划线和最多尾随一个下划线的形式进行声明(如“__spam”&#xff0c;这只是惯例&#xff0c;而不是Python的强制要求)。当然&#xff0c;我们也可以给类的实例取任意名称。例如&#xff1a;然&#xff0…

xml配置文件显示为文本文件问题

idea 新建的xml文件显示为文本问题 原因: 由于新建不带后缀名的文件的时候 idea会相对智能的让你选择 文件规则 解决: settings->File types 中找到对应的文件类型显示 ,把 你不小心添加的 正则 给去除就好了, 我这里的配置如下图 可以自己进行设置&#xff08;&…

Java IO: System.in, System.out, System.err

转载自 Java IO: System.in, System.out, System.err译文链接 作者: Jakob Jenkov 译者: 李璟(jlee381344197gmail.com) System.in, System.out, System.err这3个流同样是常见的数据来源和数据流目的地。使用最多的可能是在控制台程序里利用System.out将输出打印到控制台上。 …

.NET应用迁移到.NET Core--调查案例

上周已经发过三篇文章讲述做.NET 应用迁移到.NET Core的一般方法&#xff0c;具体内容请看&#xff1a; .NET应用迁移到.NET Core&#xff08;一&#xff09; .NET应用迁移到.NET Core&#xff08;二&#xff09;风险评估 .NET应用迁移到.NET Core&#xff08;三&#xff09;从…