如果内容高度小于屏幕高度,则在 ScrollView 的底部强制显示一个视图
Force a View on the bottom of ScrollView if content height less than screen height
我有一个带有一些视图的 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>
添加以下代码
<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">
您可以使用 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>
试试这个:
屏幕截图
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>
在 ScrollView 中使用下面的 class 而不是 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)
}
}
}
我有一个带有一些视图的 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>
添加以下代码
<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">
您可以使用 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>
试试这个:
屏幕截图
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>
在 ScrollView 中使用下面的 class 而不是 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)
}
}
}