【问题标题】:Android LinearLayout does not left align contentsAndroid LinearLayout 不左对齐内容
【发布时间】:2014-12-08 01:46:21
【问题描述】:

我想在显示屏顶部创建一条水平线的小图像缩略图。它们应该从左排列写(全部向左浮动)。当您单击缩略图时,其较大的版本应显示在此行下方。我决定使用 horizo​​ntal 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>

但问题是图像显示在布局的中心,如您在此处看到的:

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

你们知道我做错了什么吗?谢谢你的帮助!!

【问题讨论】:

    标签: android-linearlayout layout-gravity


    【解决方案1】:

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

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

    效果很好:-)

    【讨论】:

      猜你喜欢
      • 2013-10-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-18
      • 2019-11-12
      • 2018-12-14
      • 1970-01-01
      • 2011-08-19
      相关资源
      最近更新 更多