Android LinearLayout没有左对齐内容

时间:2014-12-07 15:48:22

标签: android-linearlayout layout-gravity

我想在显示器顶部创建一个水平线的小图像缩略图。它们应该从左边排成一行(所有浮动的左边)。当您单击缩略图时,其较大的版本应显示在此行下方。我决定使用水平LinearLayout (参见ID = galleryLayout的那个)。它应该是一个LinearLayout,因为我想稍后在 Horizo​​ntalScrollView 中使用它。这是我的布局代码:

<?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>

但问题是图像显示在布局的中心,如下所示:

enter image description here

正如您所看到的,LinearLayout中的重力不起作用,图像水平显示在中心。 ImageView本身的重力也不起作用(这只是尝试使它工作!)。

你们有什么想法我做错了吗?谢谢你的帮助!!

1 个答案:

答案 0 :(得分:0)

我可以设法逐步解决问题。结果如下:

<?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>

完美无缺: - )