当我将ImageView的可见性设置为GONE LinearLayout时,它的一个子项保留,另一个在android中消失

费哈德(Mehdizade):

我将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消失了,另一个仍然存在。我已经读过类似于此ConstraintLayout的内容,当依赖约束的视图消失时,布局视图的行为很奇怪但是在我的情况下,如果这是线性布局的子视图(为什么所有线性布局都没有消失?),为什么仍然保留文本视图之一。

应用中的布局(我将英文文本更改为词组,但未看到miwok): 在此处输入图片说明

Cheticamp:

Because the LinearLayout has its top and bottom constraints set to the ImageView, it will be centered vertically on the ImageView. See the documentation that addresses this. "Vertically centered" means that the vertical center of the LinearLayout and the ImageView will be at the same y position.

Now, when the ImageView is set to GONE, the ImageView is reduced to a virtual point. Since it is constrained to the start and top of the parent, the "gone" view will now be a point at the screen's origin (0,0). The LinearLayout remains constrained to the top and bottom of the ImageView as before, so it is still centered on the ImageView. But, since the ImageView is now a point at the origin, and its centered has shifted, the LinearLayout must shift up to keep centered on the ImageView. As a result, the top TextView leaves the top of the screen and can no longer be seen.

您如何解决此问题取决于您要执行的操作。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章