【问题标题】:How to expand the ScrollView vertically if the height is smaller than the screen in Android? (xml / programmatically)如果高度小于Android中的屏幕,如何垂直扩展ScrollView? (xml /以编程方式)
【发布时间】:2021-06-13 03:18:55
【问题描述】:

我是 Android 新手,我需要使用 ScrollView包装我的整个内容,因为 在某些情况下它需要占用更多的高度比屏幕上显示的要多大多数情况内容的高度小于屏幕ScrollView几乎总是有一个背景颜色(不是白色),它需要填满整个屏幕可用,而不仅仅是包装内容。我检查了一些与此相关的其他主题,但答案都已过时,而且似乎都没有解决问题,甚至都没有关注所提出的问题。

额外细节: ScrollView内部有一个封装内容的RelativeLayout,因为可以有ScrollView 中只有一个元素。

如果他们不使用编程方法、Kotlin 或任何其他用于 Android 编程的语言,请将答案限制在 Java for Android Studio 或 XML 配置。提前谢谢你!

<?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>

【问题讨论】:

    标签: java android android-studio scrollview height


    【解决方案1】:

    您的问题实际上是关于如何设置宽度和高度。

    • MATCH_PARENT - 和父母的容器一样大。
    • 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">
    

    【讨论】:

    • 代码中的错误是 android:layout_height 的值,它需要是 "match_parent",正如您在此处显示的那样。感谢你的回答。 (不过,RelativeLayout 需要保持 android:layout_height="wrap_content",以便滚动工作)
    【解决方案2】:

    只需将您的ScrollView 放入ConstraintLayout,然后设置ScrollViewandroid: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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-04-01
      • 1970-01-01
      • 2018-11-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多