[英]How to make TextView fill space vertically in ScrollView?
我有以下布局。 我有 FrameLayout(紫紅色)填充整個屏幕,左側有 ScrollView(銀色)。 在 ScrollView 是帶有按鈕的 LinearLayout (lime)。 底部按鈕上方是 TextView 間隔。 我希望它填充垂直空間,以便底部按鈕位於屏幕底部。 使用滾動視圖滾動工作但間隔沒有拉伸:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff">
<ScrollView
android:layout_width="134dp"
android:layout_height="match_parent"
android:background="#aaaaaa">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Spacer" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bottom" />
</LinearLayout>
</ScrollView>
</FrameLayout>
如果我刪除 ScollView,那么間隔會被拉伸,但在橫向模式下滾動不起作用:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff">
<LinearLayout
android:layout_width="134dp"
android:layout_height="match_parent"
android:background="#00ff00"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Spacer" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bottom" />
</LinearLayout>
</FrameLayout>
解決方案:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff00ff">
<ScrollView
android:layout_width="134dp"
android:layout_height="match_parent"
android:fillViewport="true"
android:background="#aaaaaa">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00ff00"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Spacer" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bottom" />
</LinearLayout>
</ScrollView>
</FrameLayout>
在 ScrollView 上設置android:fillViewport="true"
解決了您的問題並將 ScrollView 的高度設置為 match_parent。
我認為約束布局會對你有所幫助。
<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"
android:orientation="vertical">
<ScrollView
android:layout_width="134dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="@+id/btnBottom"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="134dp"
android:layout_height="wrap_content"
android:background="#00ff00"
android:orientation="vertical">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="7" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="Spacer" />
</LinearLayout>
</ScrollView>
<Button
android:id="@+id/btnBottom"
android:layout_width="134dp"
android:layout_height="wrap_content"
android:text="Bottom"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</ConstraintLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.