【问题标题】:Equal width ImageViews in horizontal LinearLayout [duplicate]水平LinearLayout中的等宽ImageViews [重复]
【发布时间】:2014-08-26 19:24:52
【问题描述】:

我的布局中有一个 LinearLayout,其中包含三个 ImageView。它们的 layout_weight 设置为 1,因此它们都应该占据相等的空间。在 XML 中是这样说的

<LinearLayout
    android:id="@+id/buttonContainer"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_vertical|center_horizontal"
    android:orientation="horizontal" >

    <ImageView
        android:id="@+id/option1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:scaleType="fitCenter"
        android:src="@drawable/m571" />

    <ImageView
        android:id="@+id/option2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:scaleType="fitCenter"
        android:src="@drawable/m562" />

    <ImageView
        android:id="@+id/option3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_weight="1"
        android:scaleType="fitCenter"
        android:src="@drawable/m531" />
</LinearLayout>

我以编程方式每 15 秒更改一次图像。该程序在开始时从 SQLite 数据库加载大约 300 个条目。它将条目存储在 ArrayList 中并对其进行洗牌。然后,每隔 15 秒,程序会获取 ArrayList 中的第一个条目并将其删除,因此它不能被使用两次。它使用 ArrayList 中的对象来更改图像。

这是代码。

// Get the first sign in the list and remove it
RoadSign sign = signs.get(0);
signs.remove(0);

// Get the possible options
List<String> options = Arrays.asList(sign.getOptions().split(","));
// Shuffle the list
Collections.shuffle(options, new Random(System.nanoTime()));

// Create a new ArrayList with two wrong choices and the correct one
ArrayList<String> choices = new ArrayList<String>();
choices.add(options.get(0));
choices.add(options.get(1));
choices.add(sign.getNumber());

// Shuffle the list
Collections.shuffle(choices, new Random(System.nanoTime()));

// Get image resources
int option1Image = getResources().getIdentifier("mypackage:drawable/m" + choices.get(0), null, null);
int option2Image = getResources().getIdentifier("mypackage:drawable/m" + choices.get(1), null, null);
int option3Image = getResources().getIdentifier("mypackage:drawable/m" + choices.get(2), null, null);

// Show the next images
option1.setImageResource(option1Image);
option2.setImageResource(option2Image);
option3.setImageResource(option3Image);

// Change the sign in 15 seconds
handler.postDelayed(runnable, 15000);

问题是图像并非每次都正确显示。图像的大小不同,但它们在布局中仍应采用相同的宽度。在某些情况下,图像很好,每个图像都占用 33% 的宽度。

这是正确位置的图片

虽然有时会发生这种情况。

在某些情况下,仅显示两个图像。有时这三个都显示,但有一个非常小,一个中型和一个巨大的。

我需要做什么才能正确显示图像?

【问题讨论】:

标签: java android android-layout android-imageview


【解决方案1】:

要使 weight 起作用,受影响的尺寸(在本例中为宽度)必须为 0dp
所以,改变这个:

android:layout_width="wrap_content"

到这里:

android:layout_width="0dp"

在您的 imageView 定义中(全部三个)

【讨论】:

  • 值得一提的原因:LinearLayout 根据其权重在其子项之间分配可用的可用空间量 - 因为您允许 ImageViews 与其内容一样大(wrap_content),所以没有任何要分配的可用空间。如果将它们的宽度设置为 0,则剩余的整个宽度将被平均分割(因为它们的权重都为 1)。
  • +1 详细说明 ;)
  • 感谢您的回答。我仍然得到相同的效果:有时其中一个图像非常小,有时只显示两个。我的布局现在看起来像这样。 pastebin.com/M66knfF3
  • 完美运行...很好的答案
  • @DragonFire 很高兴你发现它有用!
【解决方案2】:

在父布局中添加 android:weightsum="3"

并在 imagview 中添加 android:layoutwidth="0dp"

【讨论】:

  • 父级中的weightSum 可选
【解决方案3】:

制作图片视图属性

android:layout_weight="0.33"

对于完整的线性布局

android:layout_weight="1"

这应该可以解决您的问题

【讨论】:

  • 不,我不......问题是线性布局是图像视图的父级,考虑到它是 100% 通过传递 1 到它的“权重”属性
  • 和 33% 到其布局的每个子级..
  • 所以,他们父母中孩子的体重 Sum 必须是 1... ;) 0.33 + 0.33 + 0.33(确实应该是 0.34)
  • 没必要,但也可以这么认为..
  • 这样,就可以去掉父级的layout_weight="1"了。
【解决方案4】:

当您使用 weight

如果被vertical布局使用,那么它的高度必须是0dp

如果被horizo​​ntal布局使用,那么它的宽度必须是0dp

所以最后这意味着当您使用 weight 时,它的 受影响的尺寸 将是 0dp 取决于布局方向

【讨论】:

  • +1 添加详细信息
  • 不客气,亲爱的朋友
  • 您好,感谢您的回答!进行此更改后,我的问题仍然存在。 pastebin.com/M66knfF3
【解决方案5】:

感谢其他人的回答。确实我需要将 layout_width 设置为 0dp,但我还必须将父布局宽度设置为 fill_parent。现在它每次都显示所有的 ImageView。

【讨论】:

    【解决方案6】:

    要创建一个线性布局,其中每个孩子在屏幕上使用相同数量的空间,请将每个视图的 android:layout_height 设置为“0dp”(对于垂直布局)或每个视图的 android:layout_width 设置为“ 0dp”(用于水平布局)。然后将每个视图的 android:layout_weight 设置为“1”。参考:http://developer.android.com/guide/topics/ui/layout/linear.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多