java - Android:约束布局以编程方式设置约束顶部?
问题描述
我有 3 个视图: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 对标记为 View.GONE 的小部件进行了特定处理。
像往常一样,GONE 小部件不会显示并且不是布局本身的一部分(即,如果标记为 GONE,它们的实际尺寸不会改变)。
但就布局计算而言,GONE 小部件仍然是其中的一部分,但有一个重要区别:
对于布局通道,它们的尺寸将被视为零(基本上,它们将被解析为一个点)如果它们对其他小部件有约束,它们仍然会受到尊重,但任何边距都将好像等于零 在这里查看
让我用一个简单的布局来演示。
<?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>
这个布局看起来像
当您将电子邮件字段(即视图 A)设置为已消失时
当您将密码设置为 GONE 时,即 View B
这就是约束布局的神奇之处。
推荐阅读
- delphi - 为什么在调整 Delphi FMX 应用程序大小时 FormFactor.Width 和 FormFactor.Height 不会改变?
- c - 在选择结构中使用 rand() 的正确方法
- javascript - getStaticProps 中的 Next.js firebase 导致开发模式出现问题
- reactjs - React 和 Vercel:错误:命令“npm run build”以 1 退出?
- python - 如何比较 JSON 响应中的每个值并根据 python 中的某些条件过滤它们?
- javascript - Javascript:如何为 JSON 数组创建可传输对象
- rust - 如何将终端存储在结构中的 tui-rs 中?
- java - java中的冒泡排序问题
- marklogic-10 - Marklogic 连接到任何应用程序
- closedxml - 使用 ClosedXML 在行为空时隐藏 DataColumn