【问题标题】:Nested LinearLayout will not left align - screen shots added嵌套的 LinearLayout 不会左对齐 - 添加了屏幕截图
【发布时间】:2013-08-12 01:02:46
【问题描述】:

我有一个水平线性布局嵌套在一个垂直线性布局中,该垂直线性布局嵌套在一个相对布局中。它有 5 个填充宽度的图像视图,我使用重量来均匀地间隔。我在运行时随机填充 1 到 5 个图像视图。它们保持均匀间隔,但不是拥抱父母的左侧,我在文档中读到这是默认设置,而是拥抱右侧。我已经尝试过重力:左和 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 文件的其余部分。提前感谢您的帮助!

编辑:添加屏幕截图。

【问题讨论】:

  • 能否请您发布所需的样本和您现在拥有的样本。抱歉,我听不懂。
  • 当然!我认为屏幕截图会有所帮助。给我几分钟让我发布一个。
  • 仍在弄清楚图像描述的工作原理。我添加了它们,但它们没有出现。第一个是 5 个 ImageViews 中的 2 个被占用并在我希望它们与左侧对齐时向右对齐。第二个是所有 5 个 ImageView 都被占用并正确对齐。
  • Milanix - 感谢您的留言。不过,我在这里谈论的是图像描述=)
  • "android:contentDescription" 未显示在 ImageView 上,它仅用于可访问性目的。如果你想同时显示文本和图像,最简单的选择是使用 TextView 和 "android:drawable"

标签: android android-linearlayout leftalign


【解决方案1】:

感谢cmets!每一个小见解都有帮助 =) 我不知道为什么他们坚持向右,我不会做任何事情都可以迫使他们向左,结果证明在我的代码中我有

imgview.bringToFront();

作为一个临时修复,当我在弄清楚布局时,在删除那条线后,它们正确地粘在了左边。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 1970-01-01
    • 2012-01-12
    • 1970-01-01
    • 1970-01-01
    • 2018-01-02
    相关资源
    最近更新 更多