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

2024-02-23

我有这样的视图层次结构

ScrollView
        ConstraintLayout
            ImageView

            LinearLayout
                TextView
                TextView
                TextView
                ...

我想要实现的是 ImageView 垂直占据可见屏幕的 65%...但由于根视图是 ScrollView,ImageView 占据可滚动区域的 65%...我希望 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>

提前致谢


在滚动视图内时,无法将高度设置为百分比。

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

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

android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,1:1" //change ratio as per your image ratio
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

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

随机推荐