滚动型和layout_constraintHeight_max使视野中消失

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

我有一个ScrollView作为容器ConstraintLayout组成的布局。这个容器的一些看法,我想对这些意见有一个最大高度,所以我用layout_constrainedHeight_max,但随后的视图空空应用程序时,只需disapear ...

下面是我的XML示例:

<ScrollView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true">

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:isScrollContainer="true">

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="8dp"
            app:layout_constrainedHeight="true"
            app:layout_constraintHeight_max="20dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:srcCompat="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="0dp"
            android:layout_height="1500dp"
            android:layout_margin="8dp"
            android:background="@android:color/darker_gray"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView1"/>

    </android.support.constraint.ConstraintLayout>

</ScrollView>

我简化了XML,只剩下重现错误与最少的组件。我试过很多东西,但imageView1只是不停地消失,我不明白为什么。 fillViewportisScrollContainer什么都没有改变,我使用约束布局的1.1.3版本。如果constraintlayout不在ScrollView ImageView1会出现,或者如果我不使用layout_constraintHeight_max(安卓maxHeight不工作),但两者的结合是行不通的。所以我缺少什么?

android android-layout
1个回答
0
投票

事实证明,对于layout_constraintWidth_maxlayout_constraintHeight_max工作(左,右,上,下)需要设置的所有限制。它并不适用于作为imageView1在具有可变高度和与其有关的其它视图的顶部的视图。

我假设你想使你的imageView1有20dp的最大高度。为了实现这一点试试这个代码:

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="8dp"
    android:layout_marginTop="8dp"
    android:layout_marginEnd="8dp"
    android:adjustViewBounds="true"
    android:maxHeight="20dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:srcCompat="@mipmap/ic_launcher" />

这里的变化是:

  1. 加入android:maxHeight="20dp"android:adjustViewBounds="true"
  2. 除去app:layout_constrainedHeight="true"app:layout_constraintHeight_max="20dp"

我也建议您设置layout_heightConstraintLayout属性wrap_content作为ScrollView假设它包含一个孩子比自己大。

© www.soinside.com 2019 - 2024. All rights reserved.