当根视图是 ScrollView 时,如何正确地使 ConstraintLayout 中的视图占据可见区域的百分比高度 [英] How to properly make a view inside ConstraintLayout take percent height of visible area when root view is a ScrollView

查看:14
本文介绍了当根视图是 ScrollView 时,如何正确地使 ConstraintLayout 中的视图占据可见区域的百分比高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这种视图层次结构

ScrollView
        ConstraintLayout
            ImageView

            LinearLayout
                TextView
                TextView
                TextView
                ...

我想要实现的是 ImageView 在垂直方向上占据了可见屏幕的 65%……但是由于根视图是 ScrollView,ImageView 占据了可滚动区域的 65%……我想要 ImageView占可见区域的 65%.

What I want to achieve is that the ImageView takes like 65% percent of the visible screen vertically... but since the root view is a ScrollView, the ImageView takes 65% of the scrollable area... I want the ImageView takes 65% of visible area.

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

What would be the best way to proced in order to get something like this?

源代码:

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

提前致谢

推荐答案

在 scrollview 中,您不能将高度设置为百分比.

You can't set height as percentage when inside scrollview.

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

I have suggest you use Aspect ratio to match your requirements.

在你的图像视图中设置高度 0dp &设置高度的尺寸比例.

In your imageview set height 0dp & set dimension ratio for height.

android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,1:1" //change ratio as per your image ratio

这篇关于当根视图是 ScrollView 时,如何正确地使 ConstraintLayout 中的视图占据可见区域的百分比高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆