如果第二个 Textview 可见性消失,如何将文本视图放在布局的中心?

How to put text view in the center of the layout if second Textview visibility is gone?

如果第二个 Textview 可见性消失,如何将文本视图放在布局的中心?

我附上了我手绘的布局。

我里面有两个 Textview 让我们说 ConstraintLayout

我想写一个 xml 在有两个可见 TextView 的情况下根据屏幕截图中的数字设置边距和约束 但是 以防 TextView2 可见性消失我想将 TextView1 放在布局的中心。

有人可以帮我吗?

  <?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"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView>
        android:id="@+id/TextView1”
        android:layout_width=“100dp”
        android:layout_height="wrap_content"
        android:text=“TextView”1
        app:layout_constraintStart_toStartOf="parent"
        android:layout_marginStart=“16dp"
        />

 <TextView>
        android:id="@+id/TextView2”
        android:layout_width=“100dp”
        android:layout_height="wrap_content"
        android:text=“TextView”1
        app:layout_constraintEnd_toEndOf=“parent”
        app:layout_constraintStart_toStartOf=“@id/TextView1”
        android:layout_marginStart=“20dp"
        android:layout_marginEnd“8dp"
        />
</androidx.constraintlayout.widget.ConstraintLayout>

add or remove visibility property

 <LinearLayout
    android:layout_width="match_parent"
    android:weightSum="2"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:layout_height="wrap_content"
        android:text="Hello1"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_weight="1"
        android:gravity="center"
        android:visibility="gone"
        android:layout_height="wrap_content"
        android:text="Hello2"/>

    </LinearLayout>

将两个 TextView 放在一个 horizontal chain 中,并将链样式设置为 spread_inside。现在,当右边的 TextView 设置为 gone 时,左边的 TextView 假设右边的约束 TextView,新边距设置为 16dp (app:layout_goneMarginEnd="16dp" ).

Margins when connected to a GONE widget

<androidx.constraintlayout.widget.ConstraintLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/TextView1"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:text="TextView1"
        app:layout_constraintEnd_toStartOf="@+id/TextView2"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintHorizontal_chainStyle="spread_inside"
        app:layout_goneMarginEnd="16dp" />

    <TextView
        android:id="@+id/TextView2"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="TextView2"
        app:layout="@id/TextView1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/TextView1"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

你应该水平线性布局。

 <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
        android:layout_centerHorizontal="true">
     <TextView>
        android:id="@+id/TextView1”
        android:layout_width=“match_parent”
        android:layout_height="wrap_content"
        android:text=“TextView”1
        android:layout_marginRight="20dp"
 android:layout_weight="0.5"
        
        />

 <TextView>
        android:id="@+id/TextView2”
        android:layout_width=“match_parent”
        android:layout_height="wrap_content"
        android:text=“TextView”1
         android:layout_marginLeft="20dp"
 android:layout_weight="0.5"
       
        />

    </LinearLayout>

像这样比其他答案简单

您可以使用 FlexboxLayout 满足此类要求

<com.google.android.flexbox.FlexboxLayout
    android:id="@+id/flexBox"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="@dimen/_15sdp"
    android:background="@null"
    app:justifyContent="space_around">

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/ContactTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/nameTitle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"/>

</com.google.android.flexbox.FlexboxLayout>

依赖性 - implementation 'com.google.android:flexbox:2.0.1'