如果内容高度小于屏幕高度,则强制在ScrollView底部显示视图

时间:2017-12-27 08:57:31

标签: android scrollview

我有一个带有一些视图的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>

4 个答案:

答案 0 :(得分:3)

添加以下代码

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

在按钮视图之前,你想要linnerLayout的底部

+

将LinearLayout的layout_height更改为match_parent

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

+

android:fillViewport="true"添加到scrollview

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

答案 1 :(得分:1)

您可以将$(document).ready(function() { $('#downpayment').on('keydown', function () { GrandTotal = $("#GrandTotal").val(); downpayment = $("#downpayment").val(); GrandTotal = parseInt(GrandTotal); downpayment = parseInt(downpayment); var value = parseInt(GrandTotal) - parseInt(downpayment); var status = ''; if (GrandTotal < downpayment) { status = "PAID OFF"; $('#status').val(status); } else { status = "MINUS " + value; $('#status').val(status); } console.log(status); }); 用作RelativeLayoutScrollView的Root子项。以下是示例。

android:fillViewport="true"

答案 2 :(得分:1)

试试这个:

截屏

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>

答案 3 :(得分: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)
        }
    }
}