Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I align image vertical center in a Linear Layout

Tags:

android

I have the following layout, which is 1 icon on the left and 2 text views (stack on top of each other) on the right. I would like to have the text1 vertically center in the panel and when I make the text2 Visible.GONE. Can you please tell me how can i do that?

<RelativeLayout android:id="@+id/panel"
        android:layout_width="fill_parent"
        android:layout_height="?android:attr/listPreferredItemHeight">
    <ImageView
        android:id="@+id/icon1"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_gravity="center_vertical"/>
    <TextView android:id="@+id/text1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/icon1"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:layout_gravity="center_vertical"/>
    <TextView android:id="@+id/text2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/icon1"
        android:layout_below="@id/text1"
        android:textAppearance="?android:attr/textAppearanceSmall"/>
    </RelativeLayout>
like image 911
hap497 Avatar asked Jan 21 '10 22:01

hap497


People also ask

How do you center a vertical linear layout?

To center align LinearLayout, assign android:gravity attribute of this LinearLayout with the value “center”. Let us create an Android application with LinearLayout containing two Button widgets as children. We shall center align these children using gravity attribute.

How do you center an image in a linear layout?

Try layout_gravity and gravity attributes in the LinearLayout to make all the childs to be centered by default. This above is a splash screen activity. So there are two childs - ImageView and TextView. Both are center aligned.

How do you center align a line vertically?

To center both vertically and horizontally, use padding and text-align: center : I am vertically and horizontally centered.


1 Answers

All you have to do is, in your first example, to replace android:gravity on the ImageView by android:layout_gravity. That's all :)

like image 138
Romain Guy Avatar answered Nov 03 '22 00:11

Romain Guy