开发者

How to draw 2 PNG Images to the screen at the same time

开发者 https://www.devze.com 2023-01-07 02:12 出处:网络
I\'d like to know how to draw two PNG pictures onto the screen. My XML layout:(named paperxml.xml) <?xml version=\"1.0\" encoding=\"utf-8\"?>

I'd like to know how to draw two PNG pictures onto the screen.

My XML layout: (named 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/an开发者_开发技巧droid"
        android:id="@+id/rockid"
        android:src="@drawable/rockrepresentation"
        android:layout_alignTop="@id/paperid"
    />

</RelativeLayout>

What would be the java code to instantiate the XML layout and display both ImageViews on the screen at the same time? Simply calling setContentView(R.drawable.paperxml); crashes my application on startup.


Replace the xml with:

<?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>

Explanation:

  • RelativeLayout doesn't use android:orientation="vertical".
  • Every view should have android:layout_width and android:layout_height.
  • Add the xmlns:android thing just in the first element.


Calling setContentView(R.drawable.paperxml); is not crashing your code - it's your XML file. Macarse has the correct answer to your problem, and keep your code the same!

You may also want to look at the View Tutorials for some examples of setting up your XML and using different View objects.


I put the XML in but it only displays one ImageView Here's a screenshot of the emulator I took. i852.photobucket.com/albums/ab87/thomasjakway1/Capture.png Its worth mentioning that the file shown is paperrepresentation

If you look hard enough you will see that at the bottom there is a second very tiny image. You just need to increase the scale.

0

精彩评论

暂无评论...
验证码 换一张
取 消