Android中TextView新增行时如何固定Button位置?
你当前的布局结构是ScrollView包裹垂直LinearLayout,所有组件按顺序排列,当目标TextView新增内容导致高度增加时,后续的Button会被向下挤压移位。要固定Button位置,推荐以下几种方案:
方案一:将Button移到ScrollView外部(最简便)
把Button从ScrollView里拿出来,让外层LinearLayout同时包裹ScrollView和Button,给ScrollView设置android:layout_weight="1",这样ScrollView会占满除Button外的所有空间,Button固定在屏幕底部,不受ScrollView内内容变化影响。
修改后的完整XML代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".GameActivity"> <!-- 占满剩余空间的ScrollView --> <ScrollView android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/levelTV" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Level 1" android:textSize="14sp" android:visibility="gone"/> <TextView android:id="@+id/senTV" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="Sentence" android:textSize="24sp" android:textStyle="bold"/> <TextView android:id="@+id/transTV" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Translation" android:textAlignment="textStart" android:textSize="24sp" android:visibility="gone"/> <ImageView android:id="@+id/pic" android:layout_width="match_parent" android:layout_height="130dp" app:srcCompat="@android:drawable/btn_dialog" android:visibility="gone"/> <EditText android:id="@+id/picET" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="word in the pic" android:minHeight="48dp" android:visibility="gone"/> <ImageButton android:id="@+id/backBtn" android:layout_width="100dp" android:layout_height="wrap_content" android:layout_gravity="right" app:srcCompat="@android:drawable/ic_input_delete" tools:ignore="SpeakableTextPresentCheck" android:onClick="back" android:visibility="gone"/> <com.google.android.material.chip.ChipGroup android:id="@+id/bank" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:visibility="gone"> <com.google.android.material.chip.Chip android:id="@+id/word1" android:layout_width="100dp" android:layout_height="wrap_content" android:tag="0" android:text="word1" android:textAlignment="center" android:theme="@style/Theme.MaterialComponents.Light"/> <!-- 其他Chip组件同理添加android:visibility="gone" --> </com.google.android.material.chip.ChipGroup> </LinearLayout> </ScrollView> <!-- 固定在底部的Button --> <Button android:id="@+id/checkBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:backgroundTint="#80DEEA" android:onClick="check" android:text="הבא" android:textSize="16sp" android:textStyle="bold"/> </LinearLayout>
注:已给需要隐藏的组件添加android:visibility="gone",你可根据实际逻辑在代码中控制显示/隐藏。
方案二:改用ConstraintLayout布局(更灵活)
如果需要更灵活的布局控制,用ConstraintLayout作为根布局,将ScrollView约束到顶部和Button的顶部,Button约束到屏幕底部:
<?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" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".GameActivity"> <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="0dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintBottom_toTopOf="@id/checkBtn"> <!-- 内部LinearLayout及组件代码同方案一,此处省略重复部分 --> </ScrollView> <Button android:id="@+id/checkBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:backgroundTint="#80DEEA" android:onClick="check" android:text="הבא" android:textSize="16sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"/> </androidx.constraintlayout.widget.ConstraintLayout>
方案三:Button留在ScrollView内但固定底部(仅特殊场景用)
如果必须让Button在ScrollView内但始终显示在底部,需要用NestedScrollView配合LinearLayout的layout_weight,让目标TextView所在的容器占满剩余空间:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" tools:context=".GameActivity"> <androidx.core.widget.NestedScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!-- 目标TextView所在容器,占满剩余空间 --> <LinearLayout android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:orientation="vertical"> <TextView android:id="@+id/senTV" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="50dp" android:text="Sentence" android:textSize="24sp" android:textStyle="bold"/> <!-- 其他隐藏组件放在此处,添加android:visibility="gone" --> </LinearLayout> <!-- 固定在ScrollView底部的Button --> <Button android:id="@+id/checkBtn" android:layout_width="match_parent" android:layout_height="wrap_content" android:backgroundTint="#80DEEA" android:onClick="check" android:text="הבא" android:textSize="16sp" android:textStyle="bold"/> </LinearLayout> </androidx.core.widget.NestedScrollView> </LinearLayout>
内容的提问来源于stack exchange,提问作者Rotem
相关产品推荐
相关产品推荐

