【问题标题】:Alignment in linear layout - Android线性布局中的对齐 - Android
【发布时间】:2018-03-27 12:23:23
【问题描述】:
提供垂直线性布局,我希望垂直对齐文本中心。
<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"/>
我希望“sample1”垂直居中,注意:layout_gravity="center" 仅提供水平居中对齐,而不是垂直居中。
参考Image
【问题讨论】:
标签:
android
xml
user-interface
layout
【解决方案1】:
试试这个。
<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>
【解决方案2】:
如果你想使用相对布局。试试这个方法对你有用
<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>
输出