嵌套的LinearLayout不会保持对齐 - 添加了屏幕截图

时间:2013-08-10 01:31:26

标签: android android-linearlayout leftalign

我有一个水平的LinearLayout嵌套在一个嵌套在RelativeLayout中的垂直LinearLayout中。它有5个图像视图填充宽度,我正在使用重量均匀的空间。我在运行时随机填充1到5个图像视图。它们保持均匀间隔,但不是拥抱父母的左侧,我在文档中读到的是默认的,他们拥抱右边。我尝试过引力:left和scaleType =“fitStart”。我已经尝试将此布局嵌套在另一个相对布局中并使用alignParentLeft。使用RelativeLayout而不是LinearLayout会将图像对齐到左侧但不会均匀分布,因此不是一个选项。

以下是我的XML文件的相关部分

<RelativeLayout 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"
tools:context=".Game1" >

<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:weightSum="7">

<LinearLayout
    android:id="@+id/animal_images_row1"
    android:layout_width="fill_parent"
    android:layout_weight="1"
    android:layout_height="0dip"
    android:layout_marginTop="5dip"
    android:layout_marginBottom="5dip"
    android:layout_marginLeft="5dip"
    android:layout_marginRight="5dip"
    >
<ImageView 
    android:layout_height="wrap_content"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:id="@+id/block1"
    android:contentDescription="@string/ph"
    android:scaleType="centerInside"

    />
<ImageView 
    android:layout_height="wrap_content"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:id="@+id/block2"
    android:contentDescription="@string/ph"
    android:scaleType="centerInside" />
<ImageView 
    android:layout_height="wrap_content"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:id="@+id/block3"
    android:contentDescription="@string/ph" 
    android:scaleType="centerInside"/>
<ImageView 
    android:layout_height="wrap_content"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:id="@+id/block4"
    android:contentDescription="@string/ph"
    android:scaleType="centerInside" />
<ImageView  
    android:layout_height="wrap_content"
    android:layout_width="0dip"
    android:layout_weight="1"
    android:id="@+id/block5"
    android:contentDescription="@string/ph"
    android:scaleType="centerInside" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

为简洁起见,我删除了其余的xml文件。在此先感谢您的帮助!

编辑:添加屏幕截图。 2 of 5 ImageViews are occupied. Aligning to right when I want them aligned to left.

All 5 ImageViews occupied and correctly aligned.

1 个答案:

答案 0 :(得分:0)

感谢您的评论!每一个小小的洞察力都有帮助=)我无法弄清楚为什么他们坚持正确,我不会做任何事情可能会迫使他们向左走,事实证明,在我的代码中我有

imgview.bringToFront();

作为临时修复,而我正在计算布局,在删除该线后,它们正确地卡在左侧。