我如何以编程方式更改约束
How can i change constraint programmatically
我需要一些有关 ConstraintLayout 的帮助。
我有 3 个垂直对齐的按钮。 Button1 和 Button3 是可见的,Button2 是不可见的。
当我点击 Button1 时,Button2 应该是可见的。
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:onClick="on_expand"
android:text="Button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:text="Button"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button1" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/button1" />
如何设置
app:layout_constraintTop_toBottomOf="@id/button2"
在 Button3 上
我找到了这个
Button b1 = (Button)findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
Button b3 = (Button) findViewById(R.id.button3);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.connect("What must i set here????");
constraintSet.clone(mConstraintLayout);
constraintSet.applyTo(mConstraintLayout);
首先添加:
android:id="@+id/constraint_layout"
到您的 ConstraintLayout,然后更改:
android:onClick="on_expand"
在:
android:onClick="onExpand"
遵守 lowerCamelCase
方法命名约定。
最后这应该是您的代码:
public class MainActivity extends AppCompatActivity {
private ConstraintLayout mConstraintLayout;
private Button mButton;
private ConstraintSet mConstraintSet = new ConstraintSet();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mConstraintLayout = findViewById(R.id.constraint_layout);
mButton = findViewById(R.id.button2);
}
public void onExpand(View view) {
// If the button is already visible then you don't need to apply any constraints
if (mButton.getVisibility() == View.VISIBLE) {
return;
}
/* Otherwise:
- Make the button2 visible;
- Insert the actual constraints in the ConstraintSet
- Define a new constraint between button3 and button2 (TopToBottomOf)
- Apply it to the ConstraintLayout */
mButton.setVisibility(View.VISIBLE);
mConstraintSet.clone(mConstraintLayout);
mConstraintSet.connect(R.id.button3, ConstraintSet.TOP,
R.id.button2, ConstraintSet.BOTTOM);
mConstraintSet.applyTo(mConstraintLayout);
}
}
我需要一些有关 ConstraintLayout 的帮助。
我有 3 个垂直对齐的按钮。 Button1 和 Button3 是可见的,Button2 是不可见的。 当我点击 Button1 时,Button2 应该是可见的。
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:onClick="on_expand"
android:text="Button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="8dp"
android:text="Button"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button1" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@id/button1" />
如何设置
app:layout_constraintTop_toBottomOf="@id/button2"
在 Button3 上
我找到了这个
Button b1 = (Button)findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button2);
Button b3 = (Button) findViewById(R.id.button3);
ConstraintSet constraintSet = new ConstraintSet();
constraintSet.connect("What must i set here????");
constraintSet.clone(mConstraintLayout);
constraintSet.applyTo(mConstraintLayout);
首先添加:
android:id="@+id/constraint_layout"
到您的 ConstraintLayout,然后更改:
android:onClick="on_expand"
在:
android:onClick="onExpand"
遵守 lowerCamelCase
方法命名约定。
最后这应该是您的代码:
public class MainActivity extends AppCompatActivity {
private ConstraintLayout mConstraintLayout;
private Button mButton;
private ConstraintSet mConstraintSet = new ConstraintSet();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mConstraintLayout = findViewById(R.id.constraint_layout);
mButton = findViewById(R.id.button2);
}
public void onExpand(View view) {
// If the button is already visible then you don't need to apply any constraints
if (mButton.getVisibility() == View.VISIBLE) {
return;
}
/* Otherwise:
- Make the button2 visible;
- Insert the actual constraints in the ConstraintSet
- Define a new constraint between button3 and button2 (TopToBottomOf)
- Apply it to the ConstraintLayout */
mButton.setVisibility(View.VISIBLE);
mConstraintSet.clone(mConstraintLayout);
mConstraintSet.connect(R.id.button3, ConstraintSet.TOP,
R.id.button2, ConstraintSet.BOTTOM);
mConstraintSet.applyTo(mConstraintLayout);
}
}