【发布时间】:2019-10-04 03:17:08
【问题描述】:
我正在尝试创建一个包含三组小部件的布局。我希望一组限制在顶部,一组限制在底部,一组浮动在两组之间的空间中间。布局将在片段中使用,因此可能会被压扁,尤其是在显示键盘时。我无法弄清楚如何防止两组重叠,同时将顶部和底部限制在布局的顶部和底部。
我想了几种方法来做到这一点,但我无法弄清楚如何正确实施。
- 使用
app:layout_constraintVertical_bias或app:layout_constraintVertical_weight将小部件拉到一起或分开。 - 使用一条链,但在组之间有中断。我不知道如何将链条分开,以让不同的群体分散开来。
- 指定取决于所包含小部件的最小高度。 (它将包含在
ScrollView中,因此如果高度受到限制,它将起作用) - 创建打包链组,其中打包链位于展开链中。
或者有没有比这些更好的方法来得到我想要的?
注意:我希望顶部和底部组不适合父布局,但我不知道如何让这些组动态地与父布局保持距离也不会分散群体。我希望顶部和底部组靠近父布局,但不一定正好相反。最好让它们靠在父布局上,而不是靠近中间。理想情况下,我可以为此使用 app:layout_constraintVertical_bias 或 app:layout_constraintVertical_weight。
代码
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<EditText
android:id="@+id/edit_text_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/edit_text_horizontal_margin"
android:layout_marginRight="@dimen/edit_text_horizontal_margin"
android:layout_marginTop="@dimen/edit_text_vertical_margin"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@id/edit_text_2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:hint="Edit Text 1"
android:selectAllOnFocus="true" />
<EditText
android:id="@+id/edit_text_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/edit_text_horizontal_margin"
android:layout_marginRight="@dimen/edit_text_horizontal_margin"
android:layout_marginTop="@dimen/edit_text_vertical_margin"
android:layout_marginBottom="@dimen/edit_text_vertical_margin"
app:layout_constraintTop_toBottomOf="@id/edit_text_1"
app:layout_constraintBottom_toTopOf="@id/button"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:hint="Edit Text 2"
android:selectAllOnFocus="true" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/edit_text_2"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:enabled="false"
android:text="Button" />
<ProgressBar
android:id="@+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="@dimen/progress_bar_margin"
app:layout_constraintTop_toBottomOf="@id/button"
app:layout_constraintBottom_toTopOf="@id/clickable_text_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:visibility="gone" />
<TextView
android:id="@+id/clickable_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="@dimen/text_button_horizontal_margin"
android:layout_marginRight="@dimen/text_button_horizontal_margin"
android:layout_marginTop="@dimen/text_button_vertical_margin"
android:layout_marginBottom="@dimen/text_button_vertical_margin"
android:paddingLeft="@dimen/text_button_horizontal_margin"
android:paddingRight="@dimen/text_button_horizontal_margin"
android:paddingTop="@dimen/text_button_vertical_margin"
android:paddingBottom="@dimen/text_button_vertical_margin"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:text="Clickable Text View"
android:clickable="true"/>
</androidx.constraintlayout.widget.ConstraintLayout>
图片
这就是我所拥有的,以及我希望它的外观。 - - - - - 这里是重叠问题。
【问题讨论】:
-
@JeelVankhede 没用
标签: android android-layout android-constraintlayout android-layout-weight