如果ScrollView的内容高度小于屏幕高度,强制将视图放在ScrollView底部

5

我有一个ScrollView,并在其中添加了一些视图。如果ScrollView的内容高度小于屏幕高度,我想强制最后一个视图位于底部。我该怎么做?

<ScrollView 
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:gravity="center_horizontal">

            <View ... />

            <View ... />

            <View ... />

            <View ... /> // this one should be on bottom if LinearLayout's contents don't exceed screen height
        </LinearLayout>
</ScrollView>

你尝试过使用相对布局并将属性“align_parentBottom”设置为true吗? - Umair
4个回答

7

添加以下代码

       <Space
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1" />

将您想要放在LinearLayout底部的按钮视图放置在之前

+

将LinearLayout的layout_height更改为match_parent

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"

+

在scrollview中添加android:fillViewport="true"属性

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

3
完美运作。 - c0dehunter
这个解决方案似乎很好,但是当使用LinearLayout和RelativeLayout作为ScrollView的根子元素时,我会收到以下布局设计视图警告:这个LinearLayout应该使用android:layout_height="wrap_content"ScrollView的子元素必须将它们的layout_width或layout_height属性设置为wrap_content,而不是在滚动维度中填充父级或匹配父级问题ID:ScrollViewSize警告所说的有道理 - 为什么要创建一个与父级高度匹配的带滚动视图的子元素。这对其他人有用吗? - Matty Bradford

2
尝试这个:

屏幕截图

enter image description here

XML代码

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fillViewport="true">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:orientation="vertical">

            <TextView
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:text="Some views" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:orientation="vertical">

                <Button
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:text="Click" />

            </LinearLayout>

        </RelativeLayout>

    </ScrollView>

</LinearLayout>

1
您可以将RelativeLayout用作ScrollView的根子项,并使用android:fillViewport="true"。下面是一个示例。
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <Button
        android:layout_width="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_height="wrap_content"
        />

</RelativeLayout>


0
请使用以下类替换ScrollView内的LinearLayout。如果内容少于ScrollView的高度,则它将使LinearLayout底部的对象与底部对齐。我使用Kotlin编写了这个,请在将其转换为Java时改进我的答案。
import android.content.Context
import android.util.AttributeSet
import android.view.View
import android.widget.LinearLayout

class ResizingLinearLayout: LinearLayout {

    constructor(context: Context, attrs: AttributeSet, defStyle: Int) : super(context, attrs, defStyle)
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs)
    constructor(context: Context) : super(context)

    override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
        val targetChild = getChildAt(this.childCount - 1)
        val parentScrollViewHeight = (parent!! as View).height
        val childParams = targetChild.layoutParams as LinearLayout.LayoutParams
        val addHeight = kotlin.math.max(parentScrollViewHeight - kotlin.math.max(oldh, h - childParams.topMargin), 0)
        if(targetChild.height > 0) {
            childParams.topMargin = addHeight
            targetChild.layoutParams = childParams
            super.onSizeChanged(w, h + addHeight, oldw, oldh)
        } else {
            super.onSizeChanged(w, h, oldw, oldh)
        }
    }
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接