当根视图是 ScrollView 时,如何正确地使 ConstraintLayout 中的视图占据可见区域的百分比高度

Boe*_*oel 3 android android-layout android-scrollview android-coordinatorlayout

我有这样的视图层次结构

ScrollView
        ConstraintLayout
            ImageView

            LinearLayout
                TextView
                TextView
                TextView
                ...
Run Code Online (Sandbox Code Playgroud)

我想要实现的是 ImageView 在垂直方向上占据了可见屏幕的 65%...的可见区域。

为了得到这样的东西,最好的方法是什么?

在此处输入图片说明

源代码:

<ScrollView
  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"
  android:fillViewport="true"
  tools:context=".MainActivity">

  <android.support.constraint.ConstraintLayout
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
      android:id="@+id/header"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:scaleType="centerCrop"
      android:src="@drawable/rain"
      app:layout_constraintHeight_percent=".65"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toTopOf="parent"/>

    <LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:orientation="vertical"
      app:layout_constraintLeft_toLeftOf="parent"
      app:layout_constraintRight_toRightOf="parent"
      app:layout_constraintTop_toBottomOf="@id/header">

      <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="AAAAAA"
        android:textSize="50dp"/>

      <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="BBBB"
        android:textSize="50dp"/>

      <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="CCCCC"
        android:textSize="50dp"/>

      <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="DDDDD"
        android:textSize="50dp"/>



    </LinearLayout>
  </android.support.constraint.ConstraintLayout>

</ScrollView>
Run Code Online (Sandbox Code Playgroud)

提前致谢

Ran*_*mar 8

在滚动视图中,您不能将高度设置为百分比。

我建议您使用纵横比来满足您的要求。

在您的图像视图中设置高度 0dp 并设置高度的尺寸比。

android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,1:1" //change ratio as per your image ratio
Run Code Online (Sandbox Code Playgroud)