Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Center inside RelativeLayout

I need to center an ImageView and TextView inside a RelativeLayout. I can't figure out what I'm doing wrong.

Here is my code:

<RelativeLayout    
        android:id="@+id/teamNameLayout"
    android:layout_width="130dip"
    android:layout_height="fill_parent"
    android:orientation="horizontal"          
    android:background="@drawable/border"
    android:layout_toLeftOf="@+id/teamPosition"          
    >
    <ImageView
            android:id="@+id/teamIcon"
            android:src="@drawable/logo1"
            android:layout_width="20dip"            
            android:layout_height="fill_parent"                    
            android:scaleType="fitXY"
            android:textStyle="bold"
       android:textSize="16sp" 
             />
    <TextView  android:id="@+id/teamName"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
        android:gravity="center"                        
       android:textColor="#000000"  
       android:textStyle="bold"
       android:textSize="16sp"      
       android:text="blabla" />
    </RelativeLayout>

I've tried the "center_horizontal" with no success. I need the ImageView will be to the right of the TextView.

like image 447
ohadinho Avatar asked Aug 06 '12 11:08

ohadinho


People also ask

How do I center a layout in relative layout?

RelativeLayout attributes You can change layout_align parent like top, left, and right. android:layout_centerHorizontal : This attribute is used to center the element horizontally within its parent container. You can change layout_center like layout_centerVertical or layout_centerInParent .

Which is better RelativeLayout or ConstraintLayout?

If you have the choice start with ConstraintLayout, but if you already have your app in RelativeLayout, stay with it. That's all I have been following. RelativeLayout is very limited in functionality and many complex layouts can't be made using it, especially when ratios are involved.

Can we use linear layout in RelativeLayout inside?

We can use LinearLayout inside RelativeLayout. We can also use RelativeLayout as a Child of LinearLayout.


1 Answers

I've tried the "center_horizontal" with no success. I need the ImageView will be to the right of the TextView.

=> Try android:layout_centerInParent="true" for your TextView, and use android:layout_toRightOf="@+id/teamName in your ImageView.

like image 190
Paresh Mayani Avatar answered Oct 16 '22 07:10

Paresh Mayani