android 动态设置约束,ConstraintLayout动态添加View,改变约束

使用的ConstraintLayout版本implementation 'androidx.constraintlayout:constraintlayout:1.1.3'如果不使用androidx的话可以使用下面的版本implementation 'com.android.support.constraint:constraint-layout:1.1.3'注意:使用不同的ConstraintL...
摘要由CSDN通过智能技术生成

使用的ConstraintLayout版本

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

如果不使用androidx的话可以使用下面的版本

implementation 'com.android.support.constraint:constraint-layout:1.1.3'

注意:使用不同的ConstraintLayout版本可能会有坑,如果在使用过程中发现实现不了想要添加的约束,可以尝试改变ConstraintLayout的版本如上所示。

1. 动态添加View

第一种情况:所有的View都是动态添加

举个例子

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/clRoot"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".MainActivity">

android:id="@+id/ivLeft"

android:layout_width="100dp"

android:layout_height="0dp"

android:layout_marginStart="16dp"

android:layout_marginTop="16dp"

android:scaleType="centerCrop"

android:src="@drawable/ic_lake"

app:layout_constraintDimensionRatio="h,16:9"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintTop_toTopOf="parent" />

android:id="@+id/tvRight"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginStart="16dp"

android:layout_marginTop="16dp"

android:text="@string/lake_tahoe_title"

android:textSize="30sp"

app:layout_constraintLeft_toRightOf="@+id/ivLeft"

app:layout_constraintTop_toTopOf="parent" />

android:id="@+id/tvBottom"

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_marginStart="8dp"

android:layout_marginTop="24dp"

android:layout_marginEnd="8dp"

android:text="@string/lake_discription"

app:layout_constraintLeft_toLeftOf="parent"

app:layout_constraintRight_toRightOf="parent"

app:layout_constraintTop_toBottomOf="@+id/ivLeft" />

16e34f919e1a

初始布局.jpg

上面的布局文件中呈现的效果如图所示,接下来我们用代码的方式动态添加View,实现上面的效果。

首先在res/values文件夹下新建一个ids.xml,在ids.xml中声明我们要添加的View的控件id。

ids.xml

然后开始写代码

override fun onCreate(savedInstanceState: Bundle?) {

super.onCreate(savedInstanceState)

addViewUseLayoutParams()

}

使用ConstraintLayout.LayoutParams

private fun addViewUseLayoutParams() {

val constraintLayout = ConstraintLayout(this)

constraintLayout.id = R.id.clRoot

constraintLayout.layoutParams = ViewGroup.LayoutParams(

ViewGroup.LayoutParams.MATCH_PARENT,

ViewGroup.LayoutParams.MATCH_PARENT

)

//先设置根布局

setContentView(constraintLayout)

val ivLeft = ImageView(this)

ivLeft.id = R.id.ivLeft

ivLeft.scaleType = ImageView.ScaleType.CENTER_CROP

ivLeft.setImageResource(R.drawable.ic_lake)

val ivLeftLayoutParams: ConstraintLayout.LayoutParams = ConstraintLayout.LayoutParams(

ScreenUtil.dpToPx(this, 100), 0

)

ivLeftLayoutParams.leftToLeft = R.id.clRoot

ivLeftLayoutParams.marginStart = ScreenUtil.dpToPx(this, 8)

ivLeftLayoutParams.topToTop = R.id.clRoot

ivLeftLayoutParams.topMargin = ScreenUtil.dpToPx(this, 8)

ivLeftLayoutParams.dimensionRatio = "h,16:9"

ivLeft.layoutParams = ivLe

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
ConstraintLayout中,可以通过ConstraintSet来动态改变子控件的宽度和高度。ConstraintSet是一个用来设置约束的类,它包含了控件的位置、大小、边距等属性。 下面是一个示例代码,可以将一个TextView控件的宽度设置为原来的一半,高度设置为原来的两倍: ``` ConstraintLayout constraintLayout = findViewById(R.id.constraint_layout); TextView textView = findViewById(R.id.text_view); ConstraintSet constraintSet = new ConstraintSet(); constraintSet.clone(constraintLayout); constraintSet.constrainWidth(textView.getId(), textView.getWidth() / 2); constraintSet.constrainHeight(textView.getId(), textView.getHeight() * 2); constraintSet.applyTo(constraintLayout); ``` 在这个示例中,我们首先获取了一个ConstraintLayout对象和一个TextView控件对象,然后创建了一个ConstraintSet对象,并使用它的clone()方法从ConstraintLayout对象中复制了所有约束。接下来,我们调用constrainWidth()方法将TextView控件的宽度设置为原来的一半,调用constrainHeight()方法将高度设置为原来的两倍。最后,我们调用applyTo()方法,将约束应用到ConstraintLayout对象上,以应用宽高的改变。 需要注意的是,使用ConstraintSet来改变控件的大小时,需要先进行clone()操作,否则会覆盖掉原有的约束。同时,只有在控件已经被添加ConstraintLayout中,才能使用它的getWidth()和getHeight()方法获取控件的宽度和高度。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值