2017-10-10 398 views
1

我想用下面的代码将文本视图添加到父级的右端。但它不起作用。如何在约束布局中以编程方式实现layout_constraintEnd_toEndOf?

ConstraintSet constraintSet = new ConstraintSet(); 
    TextView mValue1 = new TextView(getContext()); 
    mValue1.setText("Value 1"); 
    mValue1.setId(R.id.rightLabel1); 
    addView(mValue1); 
    constraintSet.clone(this); 
    constraintSet.connect(mValue1.getId(), ConstraintSet.TOP, this.getId(), ConstraintSet.TOP); 
    constraintSet.connect(mValue1.getId(), ConstraintSet.END, this.getId(), ConstraintSet.END); 
    constraintSet.applyTo(this); 

回答

0

我注意到我没有设置根约束布局的id。如果我简单地设置ID,它的工作原理。或
更改

constraintSet.connect(mValue1.getId(), ConstraintSet.END, this.getId(), ConstraintSet.END); 

constraintSet.connect(mValue1.getId(), ConstraintSet.END, ConstraintSet.PARENT_ID, ConstraintSet.END); 

解决了这个问题。

相关问题