TextView in the center of the screen [duplicate]

北战南征 提交于 2019-12-03 10:25:05

If your font size is big enough, it might look like it's not centered, because of the font padding.
Try using the already mentioned properties combined with android:includeFontPadding, something like this:

    android:gravity="center"
    android:includeFontPadding="false"

If you want to align contents of RelativeLayout in the center then you need to put android:gravity="center" in the RelativeLayout's properties. Below is a sample XML code for it along with its visual representation.

XML Code:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:background="@color/white">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="My Text View"
            android:textColor="@color/black"
            android:textSize="25dp"
            android:textStyle="bold" />
</RelativeLayout>

Graphical Layout:

Rotemmiz

Try android:layout_centerInParent="true" or android:layout_centerHorizontal="true" these apply to RelativeLayout

You can also set the gravity="center" of its parent.

Orkun Ozen

Alternatively, taken from another post

<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:gravity="center_vertical|center_horizontal"
    android:text="@string/**yourtextstring**" />
android:gravity="center" 

works on RelativeLayouts only. So try

<TextView
        android:id="@+id/title"
        android:layout_width="fill_parent" <!--relative Layout-->
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text="Mint Payments"/>

This should display in center of the screen with center aligned. If you want to text alignment, you may use android:gravity="right" or android:gravity="left"

     <RelativeLayout
                xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <TextView
                    android:id="@+id/textView"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:gravity="center" 
                    android:text="This is some text" />    
    </RelativeLayout>
    <RelativeLayout
    android:gravity="center"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" >
     <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Txt is aligned in Center" />
   </RelativeLayout>

use

android:gravity="center"

attribute for your TextView in layout_XML File.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!