Подтвердить что ты не робот

TextView в центре экрана

Возможный дубликат:
Как разместить текст по горизонтали и вертикали в TextView на Android?

У меня есть RelativeLayout (до того, как он был LinerLayout), который занимает весь экран, и я хочу поместить его в центр Layout, a TextView. Я пытаюсь сделать это с помощью gravity = "center" layout_gravity = "center" и еще нескольких, но это не сработает.

Кто-нибудь знает, как центрировать TextView в середине экрана?

ИЗМЕНИТЬ

Хорошо, я думаю, что я плохо объяснил. Я думаю, что TextView находится в центре, но я хочу сосредоточиться на тексте в TextView. Могу ли я это сделать?

4b9b3361

Ответ 1

Если размер вашего шрифта достаточно велик, он может выглядеть как не центрированный из-за заполнения шрифта.
Попробуйте использовать уже упомянутые свойства в сочетании с android:includeFontPadding, примерно так:

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

Ответ 2

Если вы хотите выровнять содержимое RelativeLayout в центре, вам нужно установить android: gravity = "center" в свойствах RelativeLayout. Ниже приведен пример XML-кода для него, а также его визуальное представление.

Код XML:

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

Графический макет:

enter image description here

Ответ 3

Попробуйте android:layout_centerInParent="true" или android:layout_centerHorizontal="true" они относятся к RelativeLayout

Ответ 4

Вы также можете установить гравитацию = "центр" своего родителя.

Ответ 5

В качестве альтернативы, взято из другого сообщения

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

Ответ 6

android:gravity="center" 

работает только с RelativeLayouts. Поэтому попробуйте

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

Ответ 7

Это должно отображаться в центре экрана с выравниванием по центру. Если вы хотите выравнивать текст, вы можете использовать android: gravity = "right" или 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>

Ответ 8

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

Ответ 9

использование

android:gravity="center"

атрибут для вашего TextView в файле layout_XML.