Android 如何在 TextView 中水平和垂直居中文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/432037/
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 do I center text horizontally and vertically in a TextView?
提问by pupeno
How do I center the text horizontally and vertically in a TextView
, so that it appears exactly in the middle of the TextView
in Android
?
如何水平和垂直居中文本的TextView
,所以它正好出现在中间TextView
的Android
?
回答by Bill
I'm assuming you're using XML layout.
我假设您使用的是 XML 布局。
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/**yourtextstring**"
/>
You can also use gravity center_vertical
or center_horizontal
according to your need.
您也可以使用重力center_vertical
或center_horizontal
根据您的需要。
and as @stealthcopter commented
in java: .setGravity(Gravity.CENTER);
正如@stealthcopter 在 java 中评论的那样: .setGravity(Gravity.CENTER);
回答by Jean
android:gravity="center"
This will do the trick
这将解决问题
回答by zeevb
You can also set it up dynamically using:
您还可以使用以下方法动态设置:
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
回答by itherese
android:layout_centerInParent="true"
This works when used with a RelativeLayout where the layout's height & width are set to wrap_content.
这适用于将布局的高度和宽度设置为 wrap_content 的 RelativeLayout。
回答by Szorstki
You can also use the combination:
您还可以使用以下组合:
android:gravity="left|center"
Then, if textview width is more than "fill_parent" the text will still be aligned to left (not centered as with gravity set only to "center").
然后,如果 textview 宽度大于“fill_parent”,则文本仍将向左对齐(不居中,因为重力仅设置为“center”)。
回答by Gaganpreet Singh
Apply gravity:
应用重力:
TextView txtView = (TextView) findViewById(R.id.txtView);
txtView.setGravity(Gravity.CENTER_HORIZONTAL);
For vertical:
对于垂直:
txtView.setGravity(Gravity.CENTER_VERTICAL);
In XML:
在 XML 中:
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="@string/Hello_World"
/>
回答by Cristiano Guerra
There are two ways of doing this.
有两种方法可以做到这一点。
The first in the XML code. You need to pay attention at the Gravity
Attribute. You also can find this attribute in the Graphic Editor; it may be easier than the XML EDITOR.
XML 代码中的第一个。您需要注意Gravity
属性。您也可以在图形编辑器中找到此属性;它可能比 XML EDITOR 更容易。
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:text="Your Text"
/>
For your specific scenario, the values of gravity will be:
对于您的特定场景,重力值将是:
center_vertical|center_horizontal
In the graphical editor you will find all the possible values, even see their results.
在图形编辑器中,您将找到所有可能的值,甚至查看它们的结果。
回答by Maverick1st
If you are using TableLayout make sure to set the gravity of the TableRows to center, too. Otherwise it will not work. At least it didn't work with me until I set the gravity of the TableRow to center.
如果您使用 TableLayout,请确保将 TableRows 的重力也设置为中心。否则它将无法工作。至少在我将 TableRow 的重力设置为中心之前,它对我不起作用。
For example, like this:
例如,像这样:
<TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="center">
<TextView android:text="@string/chf" android:id="@+id/tv_chf" android:layout_weight="2" android:layout_height="wrap_content" android:layout_width="fill_parent" android:gravity="center"></TextView>
</TableRow>
回答by NagarjunaReddy
You need to set TextView Gravity(Center Horizontal & Center Vertical) like this:
您需要像这样设置TextView Gravity(水平中心和垂直中心):
android:layout_centerHorizontal="true"
and
和
android:layout_centerVertical="true"
And dynamically using:
并动态使用:
textview.setGravity(Gravity.CENTER);
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);
回答by epicness42
In my opinion,
在我看来,
android:gravity="center"
is better than,
比,
android:layout_centerInParent="true"
which is better than,
这比,
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
at least for formatting text.
至少用于格式化文本。