RelativeLayout中ScrollView设wrap_content与底部TextView间空白问题求助
Fixing the Gap Between ScrollView and Bottom TextView in BottomSheetDialog
Hey there! Let's tackle that frustrating gap between your ScrollView and the bottom TextView. The root issue here is how RelativeLayout handles the layout_above constraint—it forces the ScrollView to fill the entire vertical space between the s_text view and the bottom TextView, even when the ScrollView's content is short.
The best way to fix this is to switch to ConstraintLayout, which gives you far more control over how views behave in different content scenarios. Here's the adjusted layout that meets all your requirements:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:id="@+id/bottom_sheet" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:id="@+id/pub" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:fontFamily="sans-serif-black" android:gravity="start|center" android:maxLines="1" android:padding="16dp" android:textIsSelectable="true" android:textStyle="italic" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/title_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoLink="web" android:ellipsize="end" android:fontFamily="sans-serif-black" android:gravity="left" android:maxLines="4" android:padding="16dp" android:textAppearance="@style/TextAppearance.AppCompat.Medium" android:textStyle="bold" app:layout_constraintTop_toBottomOf="@id/pub" /> <TextView android:id="@+id/s_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:ellipsize="end" android:fontFamily="sans-serif-condensed" android:gravity="left|center" android:maxLines="2" android:padding="16dp" android:text="" android:textAppearance="@style/TextAppearance.AppCompat.Title" android:textSize="16sp" android:textStyle="normal" app:layout_constraintTop_toBottomOf="@id/title_text" /> <ScrollView android:id="@+id/scrollView" android:layout_width="match_parent" android:layout_height="wrap_content" android:fillViewport="true" app:layout_constrainedHeight="true" app:layout_constraintBottom_toTopOf="@id/downloaded_file_name" app:layout_constraintTop_toBottomOf="@id/s_text"> <TextView android:id="@+id/desc_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:autoLink="web" android:gravity="left" android:padding="16dp" android:scrollbars="vertical" android:textAppearance="@style/TextAppearance.AppCompat.Subhead" android:textColor="@color/colorPrimaryText" android:textSize="14sp" /> </ScrollView> <TextView android:id="@+id/downloaded_file_name" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="end|bottom" android:padding="6dp" android:textIsSelectable="true" android:textSize="10sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toBottomOf="@id/scrollView" /> </androidx.constraintlayout.widget.ConstraintLayout>
Key Changes Explained:
- Switched to ConstraintLayout: This layout allows us to define flexible constraints that adapt to content size.
app:layout_constrainedHeight="true"on ScrollView: This is the magic fix. When content is short, the ScrollView stays atwrap_contentheight, so the bottom TextView sits directly below it (no gap). When content is long, the ScrollView is constrained to fit betweens_textand the bottom TextView, enabling scrolling without hiding the bottom element.- Dual constraints on bottom TextView: It's anchored to both the parent bottom (so it stays at the bottom of the BottomSheet) and the ScrollView's bottom (so it follows the ScrollView when content is short).
Why Your Previous Attempts Didn't Work:
- LinearLayout with weight: This forces the ScrollView to fill remaining space even when content is short, creating the gap. Without weight, long content pushes the bottom TextView off-screen.
- RelativeLayout
layout_above: This stretches the ScrollView to fill the space to the bottom TextView regardless of content length, causing gaps for short text. Removinglayout_abovelets long content cover the bottom TextView.
内容的提问来源于stack exchange,提问作者itabdullah
相关产品推荐
相关产品推荐

