How do You Center a TextView in Layout?

 ̄綄美尐妖づ 提交于 2019-12-03 22:09:56
Brandon O'Rourke

It sounds like you want to center the text within the TextView, not the TextView in the Layout.

You're already explicitly setting the minWidth of the TextView, which is good. Have you tried using android:gravity="center_horizontal" for the TextView? NOT layout_gravity which is the gravity of the TextView within the parent, in this case LinearLayout.

Here is what finally worked for me:

        <RelativeLayout android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true">
        <TextView android:id="@+id/runway_label"
            android:layout_width="wrap_content" android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true" android:gravity="center_horizontal"
            android:minWidth="110sp" android:textColor="#66CCFF"
            android:textStyle="bold" android:textSize="15sp" android:text="@string/runway_label" />
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@id/runway_label"
            android:layout_centerHorizontal="true"
            android:orientation="horizontal">
            <ImageView android:id="@+id/dec_runway_button" android:src="@drawable/minus_button"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"/>
            <TextView android:id="@+id/runway_value"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:textColor="#FFFFFF"
                android:textStyle="bold" android:textSize="40.0sp"
                android:minWidth="50sp"
                android:layout_centerInParent="true"
                android:layout_gravity="center"
                android:shadowColor="#333333" android:shadowDx="2.0"
                android:shadowDy="2.0" android:shadowRadius="3.0" />
            <ImageView android:id="@+id/inc_runway_button" android:src="@drawable/plus_button"
                android:layout_width="wrap_content" android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"/>
        </LinearLayout>
    </RelativeLayout>

If android:layout_gravity="center_horizontal" is not working try:

        android:layout_gravity="center_horizontal"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
urnish

Just use below line..which works fine to align your TextView in center.

android:layout_gravity="center_horizontal"

Example..

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My Text View"
    android:layout_gravity="center_horizontal"
    android:textSize="28sp"/>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!