当根视图是ScrollView时,如何在ConstraintLayout中正确创建视图占用可见区域的百分比高度

问题描述 投票:2回答:1

我有这种层次的观点

ScrollView
        ConstraintLayout
            ImageView

            LinearLayout
                TextView
                TextView
                TextView
                ...

我想要实现的是ImageView垂直占据可见屏幕的65%...但由于根视图是ScrollView,因此ImageView占用可滚动区域的65%......我希望ImageView占65%可见区域。

什么是最好的方式来获得这样的东西?

enter image description here

源代码:

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

提前致谢

android android-layout android-scrollview android-coordinatorlayout
1个回答
3
投票

在scrollview内部,您无法将高度设置为百分比。

我建议您使用宽高比来满足您的要求。

在您的imageview设置高度0dp并设置高度的尺寸比。

android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,1:1" //change ratio as per your image ratio
© www.soinside.com 2019 - 2024. All rights reserved.