2017-10-16 165 views
-1

提供了垂直线性布局,我希望垂直对齐文本中心。线性布局对齐 - Android

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="sample1" 
    android:layout_gravity="center" 
    android:gravity="center"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="sample2" 
    android:layout_marginTop="150dp"/> 

我希望有“样本”在中心垂直,注:layout_gravity =“中心”只是提供了通过水平中心和垂直不对准居中。

请参阅Image

回答

0

请尝试此操作。

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

<TextView 
    android:layout_width="match_parent" 
    android:layout_height="150dp" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:text="sample1"/> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_gravity="center" 
    android:gravity="center" 
    android:text="sample2"/> 
</LinearLayout> 
+0

厂工作,感谢..! –

0

那么,如果你想走相对布局。试试这个方式,它会为你

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:id="@+id/sample1" 
     android:layout_centerVertical="true" 
     android:text="sample1"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:gravity="center" 
     android:layout_below="@+id/sample1" 
     android:text="sample2"/> 
</RelativeLayout> 

输出

enter image description here