Вы находитесь на странице: 1из 2

Android - Center TextView Horizontally in LinearLayout - Stack Overflow http://stackoverflow.com/questions/7651912/android-center-textview-hor...

sign up log in tour help stack overflow careers

Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute: ×

Android - Center TextView Horizontally in LinearLayout

I have the following basic layout

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bar_background">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textAppearance="?android:attr/textAppearanceLarge"
android:padding="10dp"
android:text="HELLO WORLD" />

</LinearLayout>
<LinearLayout>

It seems like the xml is correct but the text is aligned to the left. The textview takes up the entire width of the parent and the textview is set
to be centered. Not sure what the problem is...

android android-layout android-linearlayout layout-gravity

edited Feb 25 '14 at 11:17 asked Oct 4 '11 at 17:44


Krushna Chulet Jake Wilson
627 4 8 22.1k 43 137 209

2 try android:gravity="center" for your textview – user370305 Oct 4 '11 at 17:47

4 Answers

What's happening is that since the the TextView is filling the whole width of the inner
LinearLayout it is already in the horizontal center of the layout. When you use
android:layout_gravity it places the widget, as a whole, in the gravity specified. Instead of
placing the whole widget center what you're really trying to do is place the content in the center
which can be accomplished with android:gravity="center_horizontal" and the
android:layout_gravity attribute can be removed.

edited Dec 20 '11 at 16:02 answered Oct 4 '11 at 17:54


Joe Dan S
41.6k 8 72 104 6,272 2 20 37

1 If I understand correctly he could change "android:layout_width="fill_parent" to "wrap_content" and then use


android:layout_gravity="center_horizontal". Am I right ? – Paul Brewczynski Dec 4 '13 at 20:59

@bluesm No, the inner LinearLayout doesn't allow itself to have space that is not filled with a View (not
considering the case of an empty LinearLayout). Thus the the android:layout_width will have the same
value (after layout). Since the width of the TextView is equal to the with of the inner LinearLayout the
TextView effectively has the android:layout_gravity values of left , right , and center at the same
time. – Dan S Dec 4 '13 at 22:20

1 de 2 25/11/2015 00:17
Android - Center TextView Horizontally in LinearLayout - Stack Overflow http://stackoverflow.com/questions/7651912/android-center-textview-hor...

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@drawable/title_bar_background">

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:padding="10dp"
android:text="HELLO WORLD" />

</LinearLayout>

answered Oct 4 '11 at 17:49


JoeLallouz
662 1 5 11

Use android:gravity="center" in TextView instead of layout_gravity .

edited Jan 25 '14 at 6:41 answered Oct 4 '11 at 17:47


Ragunath Jawahar Blackbelt
7,924 12 71 118 79.5k 14 80 127

Short and precise answer. Thank you – nurne Jul 19 at 14:27

If you set <TextView> in center in <Linearlayout> then first put


android:layout_width="fill_parent" compulsory
No need of using any other gravity

<LinearLayout
android:layout_toRightOf="@+id/linear_profile"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="It's.hhhhhhhh...."
android:textColor="@color/Black"

/>
</LinearLayout>

edited May 6 '14 at 5:38 answered May 6 '14 at 5:15


Prasad Jadhav 135
1,935 10 35 61 199 1 8

2 de 2 25/11/2015 00:17

Вам также может понравиться