代码之家  ›  专栏  ›  技术社区  ›  Elad Benda

如何在垂直线性布局中垂直居中视图?

  •  0
  • Elad Benda  · 技术社区  · 7 年前

    我有一个垂直线性布局,它包装了另一个垂直线性布局。

    据我所知,垂直线性布局迫使其子对象从左上角开始定位。

    <LL vertical> //outer
    
      <LL vertical> //inner - is used to group
        <textView> //just a view to be vertically centered
        </textView>
      <LL vertical>
    
        <anotherView/>
    
    </LL vertical>
    

    当我删除外部线性布局时,我看到内部布局垂直居中于其父布局。

    如何实现相同的垂直居中?分组需要外部线性布局。

    1 回复  |  直到 7 年前
        1
  •  0
  •   Ankit Kumar    7 年前
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
    
       <LinearLayout
           android:id="@+id/layout1"
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:layout_weight="1">
    
       </LinearLayout>
    
       <LinearLayout
           android:id="@+id/inner_layout"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:gravity="center_vertical"
           android:orientation="vertical">
    
           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="Text 1" />
       </LinearLayout>
    
       <LinearLayout
           android:layout_width="match_parent"
           android:layout_height="0dp"
           android:id="@+id/layout2"
           android:layout_weight="1">
    
           <TextView
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"
               android:text="Another View" />
       </LinearLayout>
    
    </LinearLayout>
    

    可以像上面的布局一样实现。将所有视图放在布局1的内部布局中,所有这些视图都应该位于布局2中的内部布局之下