当我将 ImageView 的可见性设置为 GONE LinearLayout 时,它的一个子项仍然存在,另一个消失了 android

When i set visibility of ImageView as GONE the LinearLayout, one of child of it remains, the other gone in android

我将 ListView 项目创建为 Constraint Layout。它有一个 ImageView 被限制到父(顶部和开始)和一个 LinearLayout 被限制到这个 ImageView(从上到上,从开始到结束,从下到下)。在 Java 部分,我做了一些逻辑,在某些情况下 ImageView 是 GONE,而在其他情况下它将是可见的。这部分没有任何问题。布局是这样的:

像这样的代码:

    <?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:paddingHorizontal="@dimen/activity_horizontal_margin"
    android:paddingVertical="@dimen/activity_vertical_margin">

    <ImageView
        android:id="@+id/image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:contentDescription="@string/list_image_cd"
        android:src="@drawable/app_logo"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:id="@+id/texts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:orientation="vertical"
        app:layout_constraintStart_toEndOf="@id/image"
        app:layout_constraintTop_toTopOf="@id/image"
        app:layout_constraintBottom_toBottomOf="@id/image">

        <TextView
            android:id="@+id/miwok_word"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="miwok" />

        <TextView
            android:id="@+id/english_word"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="english" />


    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

我可以通过将布局更改为 LinearLayout 来解决问题。但是在 Constraint Layout 中,当 ImageView 消失时,其中一个 textview 消失了,另一个仍然存在。我读过类似的 。但在我的情况下,如果这是线性布局的子视图,为什么其中一个文本视图仍然存在(为什么所有线性布局都没有消失?)。

应用中的布局(我把英文文本改成了词组,但是没看到miwok):

因为 LinearLayout 的顶部和底部约束设置为 ImageView,它将在 [=18= 上垂直居中]ImageView。请参阅解决此问题的 documentation。 “垂直居中”意味着 LinearLayoutImageView 的垂直中心将在相同的 y 位置。

现在,当 ImageView 设置为 GONE 时,ImageView 缩小为一个虚拟点。由于它被限制在父级的开始和顶部,“消失”视图现在将是屏幕原点 (0,0) 的一个点。 LinearLayout 仍然像以前一样被限制在 ImageView 的顶部和底部,因此它仍然以 ImageView[= 为中心32=]。但是,由于 ImageView 现在是原点的一个点,并且它的中心已经移动,所以 LinearLayout 必须向上移动以保持在 ImageView.结果,topTextView离开了屏幕顶部,再也看不到了。

如何解决这个问题取决于您要做什么。

As your LinearLayout top and bottom constraint is given to imageview and when it's gone it's height become less and because of that top textview is not visible.

@Cheticamp give more information in his answer.

You can change your constraint like below and it will work as you want. Change Imageview top and bottom constraint to LinearLayout top and bottom.

    <?xml version="1.0" encoding="utf-8"?><?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:padding="10dp">

    <ImageView
        android:id="@+id/image"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@mipmap/ic_launcher"
        app:layout_constraintBottom_toBottomOf="@+id/texts"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/texts" />

    <LinearLayout
        android:id="@+id/texts"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:orientation="vertical"
        app:layout_constraintStart_toEndOf="@+id/image"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/miwok_word"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="miwok" />

        <TextView
            android:id="@+id/english_word"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="english" />
        
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>