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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20055863/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 02:14:50  来源:igfitidea点击:

Center content of TextView vertically & horizontally - LinearLayout

androidandroid-layouttextviewandroid-linearlayout

提问by Matt

I am trying to center the context of a TextView vertically and horizontally. Please see the below screenshot.

我试图垂直和水平居中 TextView 的上下文。请看下面的截图。

enter image description here

在此处输入图片说明

The TextViews that say 1-5and 100+ Pointsare the ones I want centered vertically and horizontally. As you can see, they are center horizontally but not vertically. Below is the code I am using.

该说的TextViews1-5100+ Points是我想要垂直和水平居中的人。如您所见,它们水平居中,而不是垂直居中。下面是我正在使用的代码。

    <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>

Again, this question is how to center the content of the TextViews, not how to center the TextViews in the LinearLayout. So, how do I center the content both ways in the TextViews?

同样,这个问题是如何将 TextViews 的内容居中,而不是如何在 LinearLayout 中居中 TextViews。那么,如何在 TextViews 中以两种方式居中内容?

回答by Evan Bashir

This is your problem:

这是你的问题:

android:layout_height="wrap_content"

Your TextView's are set to wrap_content, which means there is no space to be aligned vertically. To fix this, set a fixed row-height. In my example below, I used 50dp; however, it can be set to anything you wish.

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

P.S- android:gravity="center"handles both center_verticaland center_horizontal.

PS-android:gravity="center"处理center_verticalcenter_horizontal

Throw this code in and check it out!

把这段代码扔进去看看吧!

<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>

回答by Collin Flynn

It looks like android is centering the baseline of the text, not the text istself.

看起来 android 将文本的基线居中,而不是文本本身。

Here's a nice blog post about this annoying problem: http://www.doubleencore.com/2013/10/shifty-baseline-alignment/

这是一篇关于这个烦人问题的不错的博客文章:http: //www.doubleencore.com/2013/10/shifty-baseline-alignment/

In summay, linear layout will attempt to align child views by their text baseline, if possible. There is an xml flag to disable this behavior: android:baselineAligned, which you can try setting to false.

总之,如果可能,线性布局将尝试通过文本基线对齐子视图。有一个 xml 标志可以禁用此行为:android:baselineAligned,您可以尝试将其设置为false

回答by MrMins

You can check this post

你可以看看这个帖子

The Android View Groups employ a notion of LayoutParams that are analogous to layout constraints used by Swing Layout Managers. The LayoutParams configure the way the Android View Group lays out it's children views. In Android the GUI can be either built programmatically using Java code or using an XML file. The structure of the XML elements parallels the structure of View object you may build programatically. The XML attributes are used to configure the Layout Params.

Android 视图组采用类似于 Swing 布局管理器使用的布局约束的 LayoutParams 概念。LayoutParams 配置 Android 视图组布局其子视图的方式。在 Android 中,可以使用 Java 代码或使用 XML 文件以编程方式构建 GUI。XML 元素的结构与您可能以编程方式构建的 View 对象的结构相似。XML 属性用于配置布局参数。

And you can configure the gravity into the layout.

您可以将重力配置到布局中。