我有以下布局。注意Z
位于下方Y
,但限制在底部。之间有一个很好的间隙Y
and Z
,由多余的垂直空间提供。这是我的desired and actual当存在时的行为多余的垂直空间.
但是,当显示键盘时,我用完了多余的垂直空间。
期望的行为(没有多余的垂直空间)当我用完垂直空间时,我希望发生以下情况:X
(ScrollView
),收缩以填充剩余空间,允许Y
and Z
以全尺寸显示。
实际行为(没有多余的垂直空间) Y
反而缩小了。
我的来源如下。我如何修改它才能在两种情况下获得我想要的行为(多余的垂直空间和没有多余的垂直空间)?
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fbe9e7"
android:gravity="center"
android:text="X"
android:textSize="96sp">
</TextView>
</ScrollView>
<TextView
android:id="@+id/text_Y"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f3e5f5"
android:gravity="center"
android:text="Y"
android:textSize="96sp"
app:layout_constraintTop_toBottomOf="@+id/scrollView" />
<TextView
android:id="@+id/text_Z"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e1f5fe"
android:gravity="center"
android:text="Z"
android:textSize="96sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_Y"
app:layout_constraintVertical_bias="1" />
</androidx.constraintlayout.widget.ConstraintLayout>
这个问题很大程度上源于X
滚动视图需要0dp
当垂直空间有限时,但是wrap_content
当有多余的垂直空间时
Note: you can demo how the layout will behave with less vertical space by dragging the bottom right corner accordingly in the preview pane for a layout in Android Studio
try this
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="@+id/text_Y"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0"
app:layout_constraintVertical_chainStyle="packed">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fbe9e7"
android:gravity="center"
android:text="X"
android:textSize="96sp">
</TextView>
</ScrollView>
<TextView
android:id="@+id/text_Y"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#f3e5f5"
android:gravity="center"
android:text="Y"
android:textSize="96sp"
app:layout_constraintBottom_toTopOf="@+id/text_Z"
app:layout_constraintTop_toBottomOf="@+id/scrollView"
app:layout_constraintVertical_bias="0" />
<TextView
android:id="@+id/text_Z"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e1f5fe"
android:gravity="center"
android:text="Z"
android:textSize="96sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)