Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

android - align a textview to the center of another view

Tags:

i have some imagebuttons and each one has a corresponding textview, i'd like to align those textview's to the center of their corresponding imageview, i mean, like is seen on the app drawer...

 !-----!  !icon !  !_____! app  name 

this is my code, i'm using a RelativeLayout

<ImageButton     android:id="@+id/blog_button"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignParentLeft="true"     android:layout_below="@+id/logo_img"     android:layout_marginLeft="50dp"     android:layout_marginTop="51dp"     android:background="@null"     android:contentDescription="@string/blog_desc"     android:src="@drawable/blog" />  <TextView     android:id="@+id/blog_text"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignLeft="@+id/blog_button"     android:layout_below="@+id/blog_button"     android:layout_marginTop="8dp"     android:text="@string/blog_desc" /> 
like image 371
Hairo Avatar asked Oct 04 '12 00:10

Hairo


2 Answers

Wrap that in a LinearLayout and center the children, like so:

<LinearLayout     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_below="@+id/logo_img"     android:gravity="center_horizontal"     android:orientation="vertical">      <ImageButton         android:id="@+id/blog_button"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:background="@null"         android:contentDescription="@string/blog_desc"         android:src="@drawable/blog" />      <TextView         android:id="@+id/blog_text"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:text="@string/blog_desc" /> </LinearLayout> 
like image 160
Jason Robinson Avatar answered Sep 18 '22 16:09

Jason Robinson


Old post but if this can help I think it's not necessary to add an additional container.

<ImageButton     android:id="@+id/blog_button"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignParentLeft="true"     android:layout_below="@+id/logo_img"     android:layout_marginLeft="50dp"     android:layout_marginTop="51dp"     android:background="@null"     android:contentDescription="@string/blog_desc"     android:src="@drawable/blog" />  <TextView     android:id="@+id/blog_text"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_alignLeft="@+id/blog_button"     android:layout_alignRight="@+id/blog_button"     android:layout_below="@+id/blog_button"     android:gravity="center_horizontal"     android:layout_marginTop="8dp"     android:layout_marginLeft="-20dp"     android:layout_marginRight="-20dp"     android:text="@string/blog_desc" /> 

It's work for me.

like image 20
gwakamol Avatar answered Sep 19 '22 16:09

gwakamol