水平LinearLayout中的等宽ImageView

时间:2014-01-06 13:08:12

标签: java android android-layout android-imageview

我的布局中有一个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%。

以下是正确展示位置的图片

enter image description here

有时候会发生这种情况。

enter image description here

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

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

6 个答案:

答案 0 :(得分:2)

要使重量生效,受影响的尺寸(在这种情况下为宽度)必须 0dp
所以,改变这个:

android:layout_width="wrap_content"

到此:

android:layout_width="0dp"

在你的imageView定义中(所有这三个)

答案 1 :(得分:1)

在父布局中添加 机器人:weightsum = “3”

和n imageview添加 机器人:layout_width = “0dp”

答案 2 :(得分:1)

制作图像视图属性

  

android:layout_weight =“0.33”

以及完整的线性布局

  

android:layout_weight =“1”

这应该可以解决您的问题

答案 3 :(得分:1)

当您使用 重量

如果 垂直 布局使用它,则其高度必须为 0dp

如果 水平 布局使用它,则其宽度必须为 0dp

最后,这意味着当您使用 权重 时,其 受影响的维度 < em> 0dp 取决于 布局方向

答案 4 :(得分:0)

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

答案 5 :(得分:0)

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