垂直水平居中TextView的内容 - LinearLayout

13

我想要将 TextView 的内容垂直和水平居中。请参见下面的截图。

enter image description here

我想要将显示为“1-5”和“100+ Points”的 TextView 垂直和水平居中。如您所见,它们在水平方向上是居中的,但在垂直方向上不是。以下是我正在使用的代码:

    <LinearLayout
        android:id="@+id/linearlayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <LinearLayout
            android:layout_width="fill_parent"   
            android:layout_height="wrap_content"
            android:orientation="horizontal" >

            <TextView 
                android:id="@+id/l1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="2"
                android:paddingTop="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:paddingBottom="5dp"
                android:textSize="12sp"
                android:gravity="center_vertical|center_horizontal" /> 

            <TextView 
                android:id="@+id/g1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:paddingTop="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:paddingBottom="5dp"
                android:textSize="12sp"
                android:gravity="center_vertical|center_horizontal" /> 

            <TextView 
                android:id="@+id/c1"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="4"
                android:paddingTop="5dp"
                android:paddingLeft="5dp"
                android:paddingRight="5dp"
                android:paddingBottom="5dp"
                android:textSize="12sp"
                android:gravity="center_vertical|center_horizontal" />
        </LinearLayout>

再次强调,这个问题是如何使TextView的内容居中,而不是如何将TextView在LinearLayout中居中。那么,我该如何让TextView的内容两个方向都居中呢?


尝试使用android:layout_gravity。同时,注意父级视图的高度。如果它只包裹内容,则定位可能没有效果。 - Blacklight
1
使用height=match_parent,这样所有项目的高度都相同且可以居中,或者在您的水平布局中使用gravity=center。 - njzk2
3个回答

16

这是你的问题:

android:layout_height="wrap_content"

您的 TextView 设置为 wrap_content,这意味着没有空间可以垂直对齐。要解决此问题,请设置固定的行高。在下面的示例中,我使用了50dp;但是,它可以设置为任何您希望的大小。

P.S - android:gravity="center" 可以处理 center_verticalcenter_horizontal

将此代码放入并检查一下!

<LinearLayout
    android:id="@+id/linearlayout2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >

    <LinearLayout
        android:layout_width="fill_parent"   
        android:layout_height="50dp"
        android:orientation="horizontal" >

        <TextView 
            android:id="@+id/l1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="2"
            android:paddingTop="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="5dp"
            android:textSize="12sp"
            android:gravity="center" /> 

        <TextView 
            android:id="@+id/g1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:paddingTop="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="5dp"
            android:textSize="12sp"
            android:gravity="center" /> 

        <TextView 
            android:id="@+id/c1"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="4"
            android:paddingTop="5dp"
            android:paddingLeft="5dp"
            android:paddingRight="5dp"
            android:paddingBottom="5dp"
            android:textSize="12sp"
            android:gravity="center" />
    </LinearLayout>

2
看起来安卓是将文本的基线居中,而不是文本本身。
这里有一篇关于这个恼人问题的不错博客文章:http://www.doubleencore.com/2013/10/shifty-baseline-alignment/ 简而言之,如果可能的话,线性布局会尝试通过文本基线来对齐子视图。有一个XML标志可以禁用此行为:android:baselineAligned,您可以尝试将其设置为false

互联网档案馆链接 - https://web.archive.org/web/20131226144332/http://www.doubleencore.com/2013/10/shifty-baseline-alignment/ - Vadim Kotov

1
你可以检查这篇文章
Android视图组使用的LayoutParams与Swing布局管理器使用的布局约束类似。LayoutParams配置Android视图组如何布局其子视图。在Android中,GUI可以通过Java代码编程构建,也可以使用XML文件构建。XML元素的结构与您可能以编程方式构建的View对象的结构相似。XML属性用于配置Layout Params。
您还可以将重力配置到布局中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接