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"
に変更したいと思います。
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です。
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()
}
}