【问题标题】:How to make TextView fill space vertically in ScrollView?如何使TextView在ScrollView中垂直填充空间?
【发布时间】:2020-01-06 18:17:23
【问题描述】:

我有以下布局。我有 FrameLayout(紫红色)填充整个屏幕,在左侧我有 ScrollView(银色)。在 ScrollView 中是带有按钮的 LinearLayout(石灰)。底部按钮上方是 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>

【问题讨论】:

    标签: android


    【解决方案1】:

    解决方案:

    <?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。

    【讨论】:

    • 这使得垫片不会拉伸。
    • 该垫片用于填充底部按钮上方的空间。在您的示例中,它没有填充底部按钮上方的空间。
    • 你想要底部按钮上方的空间吗?
    • 是的,看第二张图片
    • 如果你想要这样的视图,那么就需要滚动视图。有什么具体原因吗?
    【解决方案2】:

    我认为constraint layout 会帮助你。

    <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>
    

    【讨论】:

    • 那只是空白屏幕。
    • 如果您使用的是 android 支持库,请将 替换为 或者如果您使用的是 androidX in您的应用请替换为
    • 我建议你约束布局而不是线性布局,因为它更强大。您可以根据自己的需求进行探索。
    猜你喜欢
    • 2010-11-23
    • 2015-05-16
    • 1970-01-01
    • 2018-10-04
    • 1970-01-01
    • 2017-02-22
    • 1970-01-01
    • 2013-03-12
    • 2018-10-17
    相关资源
    最近更新 更多