以编程方式更改约束布局中的准则百分比 [英] Change Guideline percentage in constraint layout programmatically

查看:166
本文介绍了以编程方式更改约束布局中的准则百分比的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在这样的约束布局中有一条准则

I have a guideline in constraint layout like this

<android.support.constraint.Guideline
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/guideline8"
    app:layout_constraintGuide_percent="0.5"
    android:orientation="vertical"/>

稍后我想有条件地将app:layout_constraintGuide_percent值更改为其他值.我该如何做到这一点.

Later I want to change the app:layout_constraintGuide_percent value to something else conditionally. How can I achieve this.

推荐答案

有两种方法可以做到:

此方法从ConstraintLayout获取特定参数并对其进行修改.

This method get a specific parameter from the ConstraintLayout and modify it.

Guideline guideLine = (Guideline) findViewById(R.id.your_guideline);
ConstraintLayout.LayoutParams params = (ConstraintLayout.LayoutParams) guideLine.getLayoutParams();
params.guidePercent = 0.45f; // 45% // range: 0 <-> 1
guideLine.setLayoutParams(params);

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆