【发布时间】: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% 的宽度。
这是正确位置的图片
虽然有时会发生这种情况。
在某些情况下,仅显示两个图像。有时这三个都显示,但有一个非常小,一个中型和一个巨大的。
我需要做什么才能正确显示图像?
【问题讨论】:
-
权重问题您使用的是 2 wrap_content 声明 + 1 权重,一个应该是 0,检查:stackoverflow.com/questions/3995825/…
标签: java android android-layout android-imageview