如何解决在 Android 中的 ScrollView 下方查看
我已经为这个问题苦苦挣扎了好几天,在网上找不到任何类似的问题!
这看起来很简单,乍一看实际上非常简单 - 将视图放在 ScrollView 的正下方。
我有两种情况:
- rootlayout 是RelativeLayout,我在视图中使用layout_below,我想放置在ScrollView 正下方
发生了什么?好吧,如果滚动视图中只有一些内容没有问题,但如果它是一个更大的列表,则 ScrollView 占据所有空间并且下面的视图(按钮)不再存在
注意:ScrollView 中的项目是动态添加的,并放置在作为 ScrollView 一部分的 LinearLayout 中。另请注意,按钮(draghandle)目前只是一个 Ad Hoc 按钮。
xml-layout 源码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/appinfo_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
>
<!-- TOP LAYOUT -->
<RelativeLayout
android:id="@+id/top_layout_appinfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp">
<ImageView
android:id="@+id/launcher_iv"
android:layout_width="90dp"
android:layout_height="75dp"
app:srcCompat="@drawable/mbi" />
<TextView
android:id="@+id/appname_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/launcher_iv"
android:text="APPNAMN"
android:textColor="@color/colorPrimaryDark" />
<ImageView
android:id="@+id/appinfo_settings_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/outline_more_vert_black_36" />
</RelativeLayout>
<!-- ALL DATA I SCROLLVIEW -->
<ScrollView
android:id="@+id/appinfo_scrollv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/top_layout_appinfo"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="vertical">
<androidx.gridlayout.widget.GridLayout
android:id="@+id/appinfo_gridLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:columnCount="2"
app:rowCount="12">
<!-- PACKAGE -->
<TextView
android:id="@+id/packagename_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PACKAGENAME"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/packagename_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cell2"
android:textColor="@color/colorPrimary" />
<!-- VERSION -->
<TextView
android:id="@+id/version_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="VERSION"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/version_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.000"
android:textColor="@color/colorPrimary" />
<!-- MIN SDK -->
<TextView
android:id="@+id/min_sdk_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MIN SDK"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/min_sdk_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.000"
android:textColor="@color/colorPrimary" />
<!-- MAX SDK -->
<TextView
android:id="@+id/target_sdk_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAX SDK"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/target_sdk_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.000"
android:textColor="@color/colorPrimary" />
<!-- SIZE SDK -->
<TextView
android:id="@+id/size_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SIZE"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/size_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.000"
android:textColor="@color/colorPrimary" />
<!-- ENABLED SDK -->
<TextView
android:id="@+id/enable_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ENABLED"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/enable_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="true"
android:textColor="@color/colorPrimary" />
</androidx.gridlayout.widget.GridLayout>
<!-- PERMISSIONS -->
<TextView
android:id="@+id/permissions_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PERMISSIONS"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<!-- TILL DENNA LAYOUT SKAPAS I KÄLLKODEN TEXVIEWS DYNAMISKT SOM LÄGGS PÅ DENNA LAYOUT-->
<LinearLayout
android:id="@+id/permissions_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</ScrollView>
<ImageButton
android:id="@+id/imgButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#FFFFFF"
android:src="@drawable/draghandle">
</ImageButton>
</RelativeLayout>
所以问题是我该如何解决这个问题?始终将 Button 保持在 ScrollView 下方的布局与 Fragment 的高度无关?
解决方法
选项 1(ConstraintLayout - 使用 scrollView maxHeight)
如果您希望按钮始终可见且位于 Scrollview 下方,您可以使用 ConstraintLayout 的根父级来实现此行为。您必须将按钮限制为始终低于 ScrollView,并在 ScrollView 中设置属性 app:layout_constraintHeight_max 以在文本太长时设置其最大可能高度。您甚至可以根据需要以编程方式更改此值。 xml布局如下:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/appinfo_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/holo_orange_light">
<!-- TOP LAYOUT -->
<RelativeLayout
android:id="@+id/top_layout_appinfo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<ImageView
android:id="@+id/launcher_iv"
android:layout_width="90dp"
android:layout_height="75dp"
app:srcCompat="@drawable/mbi" />
<TextView
android:id="@+id/appname_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/launcher_iv"
android:text="APPNAMN"
android:textColor="@color/colorPrimaryDark" />
<ImageView
android:id="@+id/appinfo_settings_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/outline_more_vert_black_36" />
</RelativeLayout>
<!-- ALL DATA I SCROLLVIEW -->
<ScrollView
android:id="@+id/appinfo_scrollv"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="@android:color/holo_blue_dark"
android:layout_margin="5dp"
app:layout_constraintHeight_max="500dp"
app:layout_constraintVertical_bias="0"
app:layout_constraintTop_toBottomOf="@+id/top_layout_appinfo"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<GridLayout
android:id="@+id/appinfo_gridLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:rowCount="12">
<!-- PACKAGE -->
<TextView
android:id="@+id/packagename_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PACKAGENAME"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/packagename_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cell2"
android:textColor="@color/colorPrimary" />
<!-- VERSION -->
<TextView
android:id="@+id/version_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="VERSION"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/version_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.000"
android:textColor="@color/colorPrimary" />
<!-- MIN SDK -->
<TextView
android:id="@+id/min_sdk_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MIN SDK"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/min_sdk_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.000"
android:textColor="@color/colorPrimary" />
<!-- MAX SDK -->
<TextView
android:id="@+id/target_sdk_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAX SDK"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/target_sdk_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.000"
android:textColor="@color/colorPrimary" />
<!-- SIZE SDK -->
<TextView
android:id="@+id/size_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SIZE"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/size_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.000"
android:textColor="@color/colorPrimary" />
<!-- ENABLED SDK -->
<TextView
android:id="@+id/enable_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ENABLED"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/enable_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="true"
android:textColor="@color/colorPrimary" />
</GridLayout>
<!-- PERMISSIONS -->
<TextView
android:id="@+id/permissions_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PERMISSIONS"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<!-- TILL DENNA LAYOUT SKAPAS I KÄLLKODEN TEXVIEWS DYNAMISKT SOM LÄGGS PÅ DENNA LAYOUT-->
<LinearLayout
android:id="@+id/permissions_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lorem_small"
android:textAllCaps="true"
android:textColor="@android:color/darker_gray"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<ImageButton
android:id="@+id/imgButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:layout_marginStart="5dp"
android:src="@drawable/draghandle"
app:layout_constraintHorizontal_bias="0"
app:layout_constraintTop_toBottomOf="@+id/appinfo_scrollv"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
上述布局的结果将如下所示。 第一张图片是带有小文本的样本,第二张图片是带有普通文本的样本,第三张图片是带有很长文本的样本(其中 ScrollView 的最大高度基于 app:layout_constraintHeight_max 属性,并且启用了垂直滚动).
选项 2(LinearLayout - 带有 layout_weight)
如果您想填满整个屏幕并且不想为 ScrollView 设置固定的 maxHeight,您可以简单地使用线性布局的父级(根)来做到这一点。您必须将 android:layout_weight="0" 设置为顶部相对布局和 ImageButton,并将 android:layout_weight="1" 设置为您的 ScrollView。
带有线性布局(根)的新 xml 布局如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/appinfo_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@android:color/holo_orange_light">
<!-- TOP LAYOUT -->
<RelativeLayout
android:id="@+id/top_layout_appinfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="@android:color/holo_green_dark"
android:layout_margin="5dp">
<ImageView
android:id="@+id/launcher_iv"
android:layout_width="90dp"
android:layout_height="75dp"
app:srcCompat="@drawable/mbi" />
<TextView
android:id="@+id/appname_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="@id/launcher_iv"
android:text="APPNAMN"
android:textColor="@color/colorPrimaryDark" />
<ImageView
android:id="@+id/appinfo_settings_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="@drawable/outline_more_vert_black_36" />
</RelativeLayout>
<!-- ALL DATA I SCROLLVIEW -->
<ScrollView
android:id="@+id/appinfo_scrollv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="5dp"
android:background="@android:color/holo_blue_dark">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<GridLayout
android:id="@+id/appinfo_gridLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnCount="2"
android:rowCount="12">
<!-- PACKAGE -->
<TextView
android:id="@+id/packagename_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PACKAGENAME"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/packagename_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="cell2"
android:textColor="@color/colorPrimary" />
<!-- VERSION -->
<TextView
android:id="@+id/version_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="VERSION"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/version_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.000"
android:textColor="@color/colorPrimary" />
<!-- MIN SDK -->
<TextView
android:id="@+id/min_sdk_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MIN SDK"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/min_sdk_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.000"
android:textColor="@color/colorPrimary" />
<!-- MAX SDK -->
<TextView
android:id="@+id/target_sdk_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="MAX SDK"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/target_sdk_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.000"
android:textColor="@color/colorPrimary" />
<!-- SIZE SDK -->
<TextView
android:id="@+id/size_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SIZE"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/size_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0.000"
android:textColor="@color/colorPrimary" />
<!-- ENABLED SDK -->
<TextView
android:id="@+id/enable_tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ENABLED"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<TextView
android:id="@+id/enable_tv_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="true"
android:textColor="@color/colorPrimary" />
</GridLayout>
<!-- PERMISSIONS -->
<TextView
android:id="@+id/permissions_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PERMISSIONS"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />
<!-- TILL DENNA LAYOUT SKAPAS I KÄLLKODEN TEXVIEWS DYNAMISKT SOM LÄGGS PÅ DENNA LAYOUT-->
<LinearLayout
android:id="@+id/permissions_ll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/lorem_small"
android:textAllCaps="true"
android:textColor="@android:color/darker_gray"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<ImageButton
android:id="@+id/imgButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0"
android:background="#FFFFFF"
android:src="@drawable/draghandle"/>
</LinearLayout>
小/中/大文本的结果将如下所示:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。