How to put text at the center of LinearLayout horizontally? Do I have to use layout_margin?
如何将文本水平放置在LinearLayout的中心?我必须使用layout_margin吗?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello"
android:layout_gravity="center_horizontal" // this does not work
/>
</LinearLayout>
2 个解决方案
#1
0
Add this attribute to your linearlayout.
将此属性添加到linearlayout。
<LinearLayout>
android:gravity="center_horizontal"
</LinearLayout>
or else change TextView
like below :
或者像下面一样更改TextView:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:gravity="center_horizontal"
/>
#2
1
you have to use gravity
instead of layout_gravity
你必须使用重力而不是layout_gravity
#1
0
Add this attribute to your linearlayout.
将此属性添加到linearlayout。
<LinearLayout>
android:gravity="center_horizontal"
</LinearLayout>
or else change TextView
like below :
或者像下面一样更改TextView:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello"
android:gravity="center_horizontal"
/>
#2
1
you have to use gravity
instead of layout_gravity
你必须使用重力而不是layout_gravity