首页 > 解决方案 > 当其兄弟线性布局消失时,线性布局不会在其父级中垂直居中

问题描述

我有这个xml

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:background="@color/google_blue900"
        android:orientation="vertical">
      <LinearLayout
          android:id="@+id/display_name_layout"
          android:layout_width="wrap_content"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:gravity="center_vertical"
          android:background="@color/google_red100"
          android:orientation="horizontal">
        <TextView
            android:id="@+id/account_display_name"
            style="@style/AccountDataDisplayName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/google_green50"
            tools:text="DisplayNameIsLongSoItShouldBeTruncatedAtSomePoint"/>
        <ImageView
            android:id="@+id/display_name_chevron"
            android:layout_width="@dimen/account_menu_chevron_size"
            android:layout_height="@dimen/account_menu_chevron_size"
            android:layout_marginTop="@dimen/account_menu_chevron_top_margin"
            android:layout_marginLeft="@dimen/account_menu_chevron_left_margin"/>
      </LinearLayout>
      <LinearLayout
          android:id="@+id/account_name_layout"
          android:layout_width="wrap_content"
          android:layout_height="0dp"
          android:layout_weight="1"
          android:gravity="center_vertical"
          android:background="@color/google_blue50"
          android:orientation="horizontal">
        <TextView
            android:id="@+id/account_name"
            style="@style/AccountDataAccountName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center_vertical"
            android:background="@color/google_yellow50"
            tools:text="emailisverylongaswellwewantittogettruncated@gmail.longdomain.com"/>
        <ImageView
            android:id="@+id/account_name_chevron"
            android:layout_width="@dimen/account_menu_chevron_size"
            android:layout_height="@dimen/account_menu_chevron_size"
            android:layout_marginTop="@dimen/account_menu_chevron_top_margin"
            android:layout_marginLeft="@dimen/account_menu_chevron_left_margin"/>
      </LinearLayout>
    </LinearLayout>

当我将第二个线性布局的可见性更改为消失时,

我希望上部线性布局在其线性布局父级中垂直居中。

我错过了什么,因为现在它没有垂直居中

在此处输入图像描述

标签: androidandroid-linearlayoutgravityandroid-layout-weight

解决方案


使用invisible而不是gone. 这将解决问题。它的作用是gone完全删除布局,导致布局对齐消失,而invisible只使用不可见。


推荐阅读