【问题标题】:Force a View on the bottom of ScrollView if content height less than screen height如果内容高度小于屏幕高度,则强制在 ScrollView 底部显示视图
【发布时间】:2017-12-27 08:57:30
【问题描述】:

我有一个 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 ?

标签: android scrollview


【解决方案1】:

添加以下代码

       <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
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

【讨论】:

  • 完美运行。
  • 这个解决方案似乎运作良好,但是当同时使用 LinearLayout 和 RelativeLayout 作为 ScrollView 的根子时,我确实收到以下布局设计视图警告 此 LinearLayout 应使用 android:layout_height="wrap_content" ScrollView子项必须在滚动维度中将其 layout_width 或 layout_height 属性设置为 wrap_content 而不是 fill_parent 或 match_parent 问题 id: ScrollViewSize 警告的含义是有道理的 - 为什么要创建一个与父级高度匹配的子级的滚动视图。这对其他人有何影响?
【解决方案2】:

您可以将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>

【讨论】:

    【解决方案3】:

    试试这个:

    屏幕截图

    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>
    

    【讨论】:

      【解决方案4】:

      在 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)
              }
          }
      }
      

      【讨论】:

        猜你喜欢
        • 2018-11-10
        • 2014-05-30
        • 2014-09-19
        • 1970-01-01
        • 2013-11-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-07-15
        相关资源
        最近更新 更多