Android 如何在布局中居中 TextView?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2784003/
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 You Center a TextView in Layout?
提问by Ken
I have a complex layout, part of which features a value centered over a label, with + and - buttons on either side of the value. I want the value to center between the buttons, whether it is "1" or "99". It looks fine when it's a 2-digit number like "99", but when it's a single digit the number is left-justified. How do I properly center that value?
我有一个复杂的布局,其中一部分的特点是一个以标签为中心的值,值的两边都有 + 和 - 按钮。我希望值在按钮之间居中,无论是“1”还是“99”。当它是一个像“99”这样的 2 位数字时看起来不错,但是当它是一个数字时,这个数字是左对齐的。我如何正确地将该值居中?
Here's the portion of my layout that does this:
这是我的布局中执行此操作的部分:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/runway_label"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<ImageView
android:id="@+id/dec_runway_button"
android:src="@drawable/minus_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
<TextView
android:id="@+id/runway_value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textStyle="bold"
android:textSize="40.0sp"
android:minWidth="50sp"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:shadowColor="#333333"
android:shadowDx="2.0"
android:shadowDy="2.0"
android:shadowRadius="3.0" />
<ImageView
android:id="@+id/inc_runway_button"
android:src="@drawable/plus_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
回答by Brandon O'Rourke
It sounds like you want to center the text within the TextView
, not the TextView
in the Layout.
听起来您想将文本居中放置在 中TextView
,而不是TextView
在布局中。
You're already explicitly setting the minWidth
of the TextView
, which is good. Have you tried using android:gravity="center_horizontal"
for the TextView
? NOT layout_gravity
which is the gravity of the TextView
within the parent, in this case LinearLayout
.
您已经明确设置minWidth
了TextView
,这很好。你试过使用android:gravity="center_horizontal"
forTextView
吗?NOTlayout_gravity
是TextView
父级内的重力,在本例中为LinearLayout
。
回答by Ken
Here is what finally worked for me:
这是最终对我有用的方法:
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<TextView android:id="@+id/runway_label"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true" android:gravity="center_horizontal"
android:minWidth="110sp" android:textColor="#66CCFF"
android:textStyle="bold" android:textSize="15sp" android:text="@string/runway_label" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@id/runway_label"
android:layout_centerHorizontal="true"
android:orientation="horizontal">
<ImageView android:id="@+id/dec_runway_button" android:src="@drawable/minus_button"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
<TextView android:id="@+id/runway_value"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textStyle="bold" android:textSize="40.0sp"
android:minWidth="50sp"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:shadowColor="#333333" android:shadowDx="2.0"
android:shadowDy="2.0" android:shadowRadius="3.0" />
<ImageView android:id="@+id/inc_runway_button" android:src="@drawable/plus_button"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical"/>
</LinearLayout>
</RelativeLayout>
回答by Oli
If android:layout_gravity="center_horizontal"
is not working try:
如果android:layout_gravity="center_horizontal"
不起作用,请尝试:
android:layout_gravity="center_horizontal"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
回答by urnish
Just use below line..which works fine to align your TextView
in center.
只需使用下面的行..它可以很好地对齐您TextView
的中心。
android:layout_gravity="center_horizontal"
android:layout_gravity="center_horizontal"
Example..
例子..
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Text View"
android:layout_gravity="center_horizontal"
android:textSize="28sp"/>