Android 如何将文本视图居中放置在线性布局中

Android 如何将文本视图居中放置在线性布局中,android,view,android-linearlayout,Android,View,Android Linearlayout,我有以下布局,我想让textview显示在视图的中间和中间。我怎样才能做到这一点 在图形视图中查看布局时,我尝试过调整各种重力属性,但似乎没有任何改变。我希望文本视图在中间,我已经完成了,但是在视图的中间 谢谢你,马特 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_

我有以下布局,我想让textview显示在视图的中间和中间。我怎样才能做到这一点

在图形视图中查看布局时,我尝试过调整各种重力属性,但似乎没有任何改变。我希望文本视图在中间,我已经完成了,但是在视图的中间

谢谢你,马特

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/carefreebgscaledalphajpg" >



    <TextView
        android:id="@+id/textviewdatefornocallspage"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="You have no calls for "
        android:textSize="25sp" />

</LinearLayout>

android:gravity=“center\u horizontal”
添加到线性布局中,并将文本视图的布局宽度更改为
android:layout\u width=“wrap\u content”


将文本视图的宽度设置为
wrap\u content
,并将线性布局设置为

android:gravity="center_horizontal"

在linearLayout标记中将重心设置为中心:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
     android:gravity="center"
    android:background="@drawable/carefreebgscaledalphajpg" >

听起来你想为TextView设置android:layout\u gravity

参见文档:


如果以直线方式垂直排列方向,您可以通过
android:layout\u gravity=“center”
将文本视图置于“水平中心”。要使textview垂直居中,您需要设置textview的布局高度以匹配父视图,并将android:gravity设置为“中心”。如果要在此布局中使用多个视图,应使用RelativeLayout和set
android:layout\u centerInParent=“true”

使用相对布局,它总是提供更准确、更灵活的布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/ic_launcher" />
</RelativeLayout>

在线性布局中为TextView设置
android:gravity=“center”
属性,并将文本视图的高度和宽度保持为
wrap\u content

这个解决方案对我有效。

试试这个 很简单,我尝试了很多答案,但所有的答案都不起作用……最后这个方法对我有效

                     <LinearLayout
                        android:layout_below="@+id/iv_divider"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:layout_marginTop="@dimen/_10dp">
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="@string/mycredits"
                            android:textColor="@color/colorGray"
                            android:gravity="center"
                            android:textSize="@dimen/_25dp" />
                    </LinearLayout>

在你的线性布局中设置
安卓:gravity=“center”

其他答案都不适合我。我不得不使用android:textAlignment=“center”

我的布局如下

<LinearLayout...
    <RelativeLayout...
        <RelativeLayout....
            <TextView
                android:layout_centerInParent="true"
                android:textAlignment="center"

RelativeLayout解决方案对我有效,但在TextView中,我必须对宽度和宽度使用
wrap_content
height@Jose_GD:在这两种情况下都适用:)。如果使用属性
match\u parent
作为宽度,只需添加
layout\u gravity=“center”
。就这些;)如果我将宽度设置为
match\u parent
,即使在添加
layout\u gravity=“center”
它是“android:gravity”而不是layout\u gravity,对我来说也不起作用。高度也应该是匹配的。谢谢,这里通过将高度和重量设置为中心来工作。这应该是答案
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerInParent="true"
    android:src="@drawable/ic_launcher" />
</RelativeLayout>
                     <LinearLayout
                        android:layout_below="@+id/iv_divider"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        android:layout_marginTop="@dimen/_10dp">
                        <TextView
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:text="@string/mycredits"
                            android:textColor="@color/colorGray"
                            android:gravity="center"
                            android:textSize="@dimen/_25dp" />
                    </LinearLayout>
<LinearLayout...
    <RelativeLayout...
        <RelativeLayout....
            <TextView
                android:layout_centerInParent="true"
                android:textAlignment="center"