当其同级线性布局消失时,线性布局未在其父对象中垂直居中

时间:2018-09-26 14:37:59

标签: android android-linearlayout gravity android-layout-weight

我有这个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>

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

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

我现在缺少什么,因为它没有垂直居中

enter image description here

1 个答案:

答案 0 :(得分:0)

使用invisible代替gone。这样可以解决问题。 gone所做的是,它完全删除了版面,结果版面对齐消失了,而使用invisible只是将其变为不可见。