1

我想知道如何在屏幕上绘制两张 PNG 图片。

我的 XML 布局:(命名为 paperxml.xml)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"  >

    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/paperid"
        android:src="@drawable/paperrepresentation"
    />

    <ImageView xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/rockid"
        android:src="@drawable/rockrepresentation"
        android:layout_alignTop="@id/paperid"
    />

</RelativeLayout>

实例化 XML 布局并同时在屏幕上显示两个 ImageView 的 java 代码是什么?简单地调用setContentView(R.drawable.paperxml);会使我的应用程序在启动时崩溃。

4

3 回答 3

2

将xml替换为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/layoutid"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"  >

    <ImageView android:id="@+id/paperid"
        android:src="@drawable/paperrepresentation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    <ImageView android:id="@+id/rockid"
        android:src="@drawable/rockrepresentation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

</LinearLayout>

解释:

  • RelativeLayout不使用 android:orientation="vertical"
  • 每个视图都应该有android:layout_widthandroid:layout_height
  • 在第一个元素中添加xmlns:android东西。
于 2010-07-12T23:01:18.333 回答
1

调用setContentView(R.drawable.paperxml);不会使您的代码崩溃——它是您的 XML 文件。Macarse 对您的问题有正确的答案,并保持您的代码相同!

您可能还想查看视图教程,了解一些设置 XML 和使用不同视图对象的示例。

于 2010-07-12T23:07:24.623 回答
0

我将 XML 放入其中,但它只显示一个 ImageView 这是我拍摄的模拟器的屏幕截图。i852.photobucket.com/albums/ab87/thomasjakway1/Capture.png 值得一提的是显示的文件是paperrepresentation

如果你仔细看,你会看到在底部有第二个非常小的图像。你只需要增加规模。

于 2010-11-21T03:36:19.990 回答