user3704589
user3704589

Reputation: 49

Android LinearLayout does not left align contents

I want to create a horizontal line of small images thumbnails on top of the display. They should be lined up from left to write (all floated left). When you click on a thumbnail, its bigger version should be displayed below this line. I decided to go with a horizontal LinearLayout (see the one with ID = galleryLayout). It should be a LinearLayout because I want to use it later within a HorizontalScrollView. Here is my layout code:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffef3a"
    android:orientation="vertical"
    tools:context=".Gallery01">

    <LinearLayout
        android:id="@+id/galleryLayout"
        android:orientation="horizontal"
        android:gravity="left"
        android:layout_width="match_parent"
        android:layout_height="120dp"
        android:background="#ffff3e34">

        <ImageView
            android:src="@drawable/pic02"
            android:layout_gravity="left"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <ImageView
        android:id="@+id/image1"
        android:scaleType="fitXY"
        android:layout_width="320dp"
        android:layout_height="320dp" />

</LinearLayout>

But the problem is that the image is displayed in the center of the layout as you can see here:

enter image description here

As you can see the gravity in the LinearLayout does not work and the image is displayed in the center horizontally. The gravity of the ImageView itself does not work as well (that is just an attempt to make it work!).

Do you guys have any idea what I am doing wrong? Thank you for help!!

Upvotes: 1

Views: 311

Answers (1)

user3704589
user3704589

Reputation: 49

I could manage to resolve the problem step by step. Here the results:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#000000"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <HorizontalScrollView
        android:layout_width="wrap_content"
        android:layout_height="120dp"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:background="#ffffff">
        <LinearLayout
            android:orientation="horizontal"
            android:layout_width="match_parent"
            android:layout_height="fill_parent"
            android:gravity="left"
            android:background="#000000"
            xmlns:android="http://schemas.android.com/apk/res/android">

            <ImageView
                android:src="@drawable/pic01"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:padding="10dp"
                android:background="#000000" />

            <ImageView
                android:src="@drawable/pic05"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:padding="10dp"
                android:background="#000000" />

            <ImageView
                android:src="@drawable/pic03"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:padding="10dp"
                android:background="#000000" />

            <ImageView
                android:src="@drawable/pic04"
                android:layout_width="120dp"
                android:layout_height="120dp"
                android:padding="10dp"
                android:background="#000000" />

        </LinearLayout>
    </HorizontalScrollView>

    <ImageView
        android:src="@drawable/pic01"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

It works perfectly :-)

Upvotes: 1

Related Questions