Вопрос

How to center the TextView horizontally and vertically in android. I tried using gravity:center. But text is displayed in the center vertically. I want it to be displayed horizontally too. How to do that.

Here is my code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.display.MainActivity$PlaceholderFragment" >

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/edit_mesaage"
        android:hint="@string/edit_message" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/send"
        android:id="@+id/send"
        android:onClick="displaym"/>   
     <TextView 
         android:id="@+id/display_mes"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:layout_gravity="center"/>
</LinearLayout>
Это было полезно?

Решение 2

Try changing the LinearLayout to a RelativeLayout. Then for your TextView, try

<TextView 
     android:id="@+id/display_mes"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_centerHorizontal="true"
     android:layout_centerVertical="true"/>

This usually works for me.

Другие советы

Can you use: "center_horizontal|center_vertical"?

You can also set it up dynamically using:

textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.CENTER_HORIZONTAL);

Or just

textview.setGravity(Gravity.CENTER);

Use android:gravity="center_horizontal|center_vertical"

As android:gravity affects the contents of the view you have, while the property android:layout_gravity="center" shall center the whole view layout according to the parent. So for a text in a textView to be centered use

<TextView 
     android:id="@+id/display_mes"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:gravity="center_horizontal|center_vertical"/>

Try this..

It because main LinearLayout orientation is horizontal so that happen try like below code.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="center"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.display.MainActivity$PlaceholderFragment" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <EditText
            android:id="@+id/edit_mesaage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:hint="@string/edit_message" />

        <Button
            android:id="@+id/send"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:onClick="displaym"
            android:text="send" />
    </LinearLayout>

    <TextView
        android:id="@+id/display_mes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:text="textview" />

</LinearLayout>

OR

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.display.MainActivity$PlaceholderFragment" >

    <EditText
        android:id="@+id/edit_mesaage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />

    <Button
        android:id="@+id/send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@+id/edit_mesaage"
        android:onClick="displaym"
        android:text="send" />

    <TextView
        android:id="@+id/display_mes"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="textview" />

</RelativeLayout>

You can surround your LinearLayout with a RelativLayout and slightly change the layout parameters on the LinearLayout, something like below suggestion:

<RelativeLayout
    xmlns:Android="..."
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true">

        ... 

    </LinearLayout>

</RelativeLayout>

This will ensure the internal relation between your buttons and text views are unchanged, since they are still wrapped by the LinearLayout, which in turn isn't filling the screen any more, hence, it can be centered within its new parent; the RelativeLayout.

Cheers

<TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="My text"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true" />
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top