我有 3 个 View :A、B、C。 (A 和 B 的高度相等)开始时 B 的可见性消失,C 的顶部约束是 A 的底部,因此 C 出现在 A 下方。一段时间后,我将 A 的可见性更改为消失,将 B 的可见性更改为可见。发生的情况是 C 被拖到顶部,因为 A 的可见性消失了。我想做的是将C的顶部约束设置为B的底部。我该怎么做?我需要以编程方式进行。
这是我现在所在的位置->
<?xml version="1.0" encoding="utf-8"?>
//A
<LinearLayout
android:onClick="clickedOnRecordLayout"
android:layout_marginTop="@dimen/record_layout_top_margin"
android:id="@+id/record_button_layout"
android:gravity="center"
android:elevation="@dimen/elevation_of_record_button"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@drawable/red_circle_drawable"
android:layout_width="@dimen/radius_of_record_button"
android:layout_height="@dimen/radius_of_record_button">
<ImageView
android:id="@+id/record_image"
android:src="@drawable/ic_microphone"
android:layout_width="@dimen/record_image_dimen"
android:layout_height="@dimen/record_image_dimen" />
</LinearLayout>
//B - initially its visibility is gone
<LinearLayout
android:onClick="clickedOnStartedRecordingLayout"
android:visibility="gone"
android:layout_marginTop="@dimen/record_layout_top_margin"
android:id="@+id/started_button_layout"
android:gravity="center"
android:elevation="@dimen/elevation_of_record_button"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:background="@drawable/red_circle_drawable"
android:layout_width="@dimen/radius_of_record_button"
android:layout_height="@dimen/radius_of_record_button">
<ImageView
android:id="@+id/stop_image"
android:src="@drawable/ic_stop_recording"
android:layout_width="@dimen/record_image_dimen"
android:layout_height="@dimen/record_image_dimen" />
</LinearLayout>
//C
<TextView
android:layout_marginTop="@dimen/tap_text_top_margin"
app:layout_constraintTop_toBottomOf="@id/record_button_layout"
android:id="@+id/tap_on_microphone_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="@string/tap_to_start_message"
android:textAlignment="center"
android:textColor="@android:color/black"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="@id/chronometer"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent" />
最佳答案
欢迎您使用ConstraintSet,但如果您想保持简单,我想提请您注意
ConstraintLayout has a specific handling of widgets being marked as View.GONE.
GONE widgets, as usual, are not going to be displayed and are not part of the layout itself (i.e. their actual dimensions will not be changed if marked as GONE).
But in terms of the layout computations, GONE widgets are still part of it, with an important distinction:
For the layout pass, their dimension will be considered as zero (basically, they will be resolved to a point) If they have constraints to other widgets they will still be respected, but any margins will be as if equals to zero Check it out here
让我用一个简单的布局来演示。
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<EditText // View A
android:id="@+id/email"
android:hint="email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText // View B
android:id="@+id/password"
android:hint="password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:visibility="gone"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/email" />
<EditText //View C
android:id="@+id/otp"
android:hint="otp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password" />
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:text="Login"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</android.support.constraint.ConstraintLayout>
这个布局看起来像
当您设置email 字段时,即 View A 已消失
当您将密码设置为GONE时,即 View B
这就是约束布局的魔力。
关于java - 安卓 : Constraint Layout Set Constraint top programmatically?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50596820/