[英]How to expand the ScrollView vertically if the height is smaller than the screen in Android? (xml / programmatically)
I'm new to Android and I need to use a ScrollView to wrap my whole content, since in some cases it needs to take up more height than is available on the screen .我是 Android 的新手,我需要使用ScrollView来包装我的全部内容,因为在某些情况下它需要占用比屏幕上可用的高度更高的高度。 Most of the cases though, the height of the content is smaller than the screen .
大多数情况下,内容的高度小于屏幕。 The ScrollView will almost always have a background color (not white), which needs to fill the whole screen available, not just wrap the content.
ScrollView几乎总是有一个背景颜色(不是白色),它需要填充整个可用屏幕,而不仅仅是包装内容。 I've checked a few other topics related to this, but the answers were outdated and none of them seems to solve the issue or even focuses on the question asked.
我检查了一些与此相关的其他主题,但答案已经过时,而且似乎都没有解决问题,甚至没有关注所提出的问题。
Extra details: Inside the ScrollView there is a RelativeLayout which encapsulates the content, as there can be only one element inside a ScrollView.额外细节:在ScrollView内部有一个封装内容的RelativeLayout ,因为 ScrollView 内只能有一个元素。
Please limit the answers to Java for Android Studio or XML configuration, if they don't use a programmatic approach, neither Kotlin, nor any other language used for Android programming. Please limit the answers to Java for Android Studio or XML configuration, if they don't use a programmatic approach, neither Kotlin, nor any other language used for Android programming. Thank you in advance!
先感谢您!
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/detailed_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true">
<RelativeLayout
android:id="@+id/detailed_view_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/product_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="@string/product_image"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/details_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/details_container_background"
android:clipChildren="false"
android:clipToPadding="false"
android:elevation="10dp"
android:translationY="-50dp"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/product_image">
<TextView
android:id="@+id/product_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="20dp"
android:textSize="25sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/product_name" />
</androidx.constraintlayout.widget.ConstraintLayout>
<GridLayout
android:id="@+id/params_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/details_container"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent">
<View
android:id="@+id/param_calories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="@drawable/param_with_icon"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</GridLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<com.google.android.material.progressindicator.CircularProgressIndicator
android:id="@+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:indeterminate="true"
app:indicatorColor="@android:color/background_dark"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</RelativeLayout>
</ScrollView>
Simply put your ScrollView
inside a ConstraintLayout
, and set ScrollView
's android:layout_height="0dp"
like following:只需将
ScrollView
放在ConstraintLayout
中,然后设置ScrollView
的android:layout_height="0dp"
如下所示:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/detailed_scroll_view"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
android:fillViewport="true">
...
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
Your problem is really about how you set the widths and heights.您的问题实际上是关于如何设置宽度和高度。
MATCH_PARENT
- As big as parent's container. MATCH_PARENT
- 与父容器一样大。WRAP_CONTENT
- As big as child's container or default width/height. WRAP_CONTENT
- 与孩子的容器或默认宽度/高度一样大。<!-- As big as specified container or device screen if it has no container -->
<ScrollView
...
android:layout_width="match_parent"
android:layout_height="match_parent"
...
>
<!-- As big as the ScrollView -->
<RelativeLayout
android:id="@+id/detailed_view_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.