androidandroid-layoutandroid-linearlayoutlayout-gravity

Linear layout gravity and place item


today i'm working on layout and i have a question , basic i think.

I have a linear layout and an imageview on it. I want to display the image and the left or on the right of the layout

I tried , layout_gravity It's important to precise that i want to do it on a linear and not on a relative or other stuff.

Thanks by advance , a screen to precise my question

http://hpics.li/fc67805

my linear with the image

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:orientation="horizontal" >

            <ImageView
                android:id="@+id/Reboot"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="0.3"
                android:layout_gravity="left"
                android:src="@drawable/arreter" />
</LinearLayout>

Solution

  • Use this layout to align the ImageView to either right/left

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:orientation="vertical" >
    
                <ImageView
                    android:id="@+id/Reboot"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
    
                    android:layout_gravity="left"         // you can use right also. It works.
                    android:src="@drawable/ic_launcher" />
    </LinearLayout>
    

    Your LinearLayout orientation is horizontal, so you can place the child views in top and bottom using android:layout_gravity. For horizontal LinearLayout right and left doesn't work, they work only for vertical LinearLayout. Hope this information is useful :)