【问题标题】:Android : How to programmatically set layout_constraintRight_toRightOf "parent"Android:如何以编程方式设置 layout_constraintRight_toRightOf “parent”
【发布时间】:2017-05-30 22:49:20
【问题描述】:

我在 ConstrainLayout 中有如下视图。

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:maxWidth="260dp"
        android:textColor="#FFF"
        android:textSize="16sp"
        app:layout_constraintLeft_toLeftOf="@+id/parent"
        app:layout_constraintTop_toBottomOf="@id/message_date"
        android:id="@+id/text_main"
        />

我想根据某些条件在 recycleViewHolder 中以编程方式将视图更改为 app:layout_constraintLeft_toLeftOf="@+id/parent"layout_constraintLeft_toRightOf="@+id/parent"

【问题讨论】:

标签: android android-support-library android-constraintlayout


【解决方案1】:

这是一个使用java代码将按钮设置到父视图底部的示例:

ConstraintLayout constraintLayout;
ConstraintSet constraintSet;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    constraintLayout = (ConstraintLayout) findViewById(R.id.activity_main_constraint_layout);

    Button button = new Button(this);
    button.setText("Hello");
    constraintLayout.addView(button);

    constraintSet = new ConstraintSet();
    constraintSet.clone(constraintLayout);

    constraintSet.connect(button.getId(), ConstraintSet.LEFT, constraintLayout.getId(), ConstraintSet.RIGHT, 0);
    constraintSet.constrainDefaultHeight(button.getId(), 200);
    constraintSet.applyTo(constraintLayout);

}

为了实现这样的目标,

app:layout_constraintLeft_toLeftOf="@+id/parent" 

你的java代码应该是这样的

set.connect(YOURVIEW.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.LEFT,0);

为了实现这样的目标,

layout_constraintLeft_toRightOf="@+id/parent"

你的java代码应该是这样的,

set.connect(YOURVIEW.getId(),ConstraintSet.LEFT,ConstraintSet.PARENT_ID,ConstraintSet.RIGHT,0);

在这里,我假设 android:id="@+id/parent" 是您的父级 ConstraintLayout 的 id。

【讨论】:

  • set.applyTo(layout); 这不是将这些约束应用于父级而不是TextView?应该是吗? set.applyTo(button); 或类似的东西?
  • 不知道你要将更改应用到谁,现在是时候告诉父布局了,嘿,请应用此更改。所以这应该像 set.applyTo(parent).
  • 你的代码是错误的,首先你必须在约束布局中添加按钮然后约束Set.clone(constraintLayout);将会完成。否则对按钮什么也不做
【解决方案2】:

约束集 = 新约束集();等等

反正对我没用。

已解决

  LayoutParams layoutParams = (LayoutParams) viewToChange.getLayoutParams();
    layoutParams.leftToLeft = anotherViewId;
    layoutParams.rightToRight =anotherViewId;
    layoutParams.topToTop = anotherViewId;
    layoutParams.bottomToBottom = anotherViewId;
    layoutParams.startToStart =anotherViewId;
    layoutParams.endToEnd = anotherViewId;
    viewToChange.setLayoutParams(layoutParams);

【讨论】:

  • 有谁知道为什么在某些情况下约束集不起作用?使用 LayoutParams 对我来说效果很好
  • 这个方法对我有用——我想改变 LinearLayout 参数,它的父级是 constrainLayout。谢谢
【解决方案3】:

使用id

class MyLayout(context: Context) : ConstraintLayout(context) {

    fun show() {
        val view = ImageView(context)
        addView(view)
        val params = view.layoutParams as ConstraintLayout.LayoutParams
        params.height = 100 
        params.width = 50
        params.rightToRight = id
        view.requestLayout()
    }
}

【讨论】:

  • 它从 view.layoutParams 返回 null
【解决方案4】:

当出现新的EditText 时,我需要将buttons 向下移动时,我遇到了同样的情况,在 XML 文件中它们具有如下约束:

app:layout_constraintTop_toBottomOf="@+id/first_layout"

而我需要做的就是以编程方式改变某事:

app:layout_constraintTop_toBottomOf="@+id/second_layout"

使用ConstraintLayout.LayoutParams 任务非常简单,例如

科特林:

val currentLayout = btn.layoutParams as ConstraintLayout.LayoutParams // btn is a View here
currentLayout.topToBottom = R.id.second_layout // resource ID of new parent field
btn.layoutParams = currentLayout

就是这样!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 2020-10-06
    • 2011-05-02
    • 1970-01-01
    • 2011-04-28
    相关资源
    最近更新 更多