如何在Android中的TextView中对齐文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11096085/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to align the text inside the TextView in Android?
提问by Ovidiu Latcu
I have a multiline TextView in Android. It is a child of a LinearLayout. It is already centered horizontally and I also want to have the text inside it centered.
我在 Android 中有一个多行 TextView。它是 LinearLayout 的子代。它已经水平居中,我也想让里面的文本居中。
Here's what I have now:
这是我现在所拥有的:
_ _ _ _ _ _| aaaaaaaaaaaaaaaaaaaaa
_ _ _ _ _ _| aaaaaaaa
_ _ _ _ _ _| aaaaaaaaaaaaaa
What I want is:
我想要的是:
_ _ _ _ _ _| aaaaaaaaaaaaaaaaaaaaa
_ _ _ _ _ _| aaaaaaaa
_ _ _ _ _ _| aaaaaaaaaaaaaa
Where:
在哪里:
_ _ _ _ = the space on the side of the TextView
_ _ _ _ = TextView 侧面的空间
| = the TextView edge
| = TextView 边缘
回答by Ovidiu Latcu
Did you try the gravity
attribute on the TextView
?
您是否尝试过gravity
上的属性TextView
?
android:gravity="center"
And make sure to use android:gravity
and not android:layout_gravity
.
并确保使用android:gravity
而不是android:layout_gravity
.
回答by barrel
回答by Bharat Singh
Yes, use android:gravityattribute to change the position of data inside views.
是的,使用android:gravity属性来改变视图中数据的位置。
回答by Sohail Ansari
Make the text (which you want to set into TextView) in strings.xml as following :
在 strings.xml 中制作文本(要设置为 TextView),如下所示:
<string name="content">
<center>Your text goes here.</center>
</string>
and in TextView add following attribute:
并在 TextView 添加以下属性:
<TextView
...
android:text="@string/content"/>
It will center the text into TextView horizontally.
它会将文本水平居中放置在 TextView 中。