2010-07-12 21 views
1

我想知道如何在屏幕上繪製兩張PNG圖片。如何同時在屏幕上同時繪製2張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> 

什麼是Java代碼來實例化XML佈局,並在同一時間在屏幕上顯示兩個ImageViews? 只需撥打setContentView(R.drawable.paperxml);即可在啓動時崩潰我的應用程序。

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

說明:

回答

2

與更換XML

  • RelativeLayout不使用android:orientation="vertical"
  • 每個視圖應該有android:layout_widthandroid:layout_height
  • 僅在第一個元素中添加xmlns:android
+0

我把XML,但它只顯示一個ImageView 下面是我採取的模擬器的屏幕截圖。 http://i852.photobucket.com/albums/ab87/thomasjakway1/Capture.png 值得一提的是,顯示的文件是paperrepresentation – dragonwrenn 2010-07-13 00:53:44

+0

這是因爲大小。嘗試使用'layout_width'和'layout_height'使用類似「40dp」的東西。 還要記住LinearLayout被設置爲Vertical。 – Macarse 2010-07-13 00:59:01

1

調用setContentView(R.drawable.paperxml);不會崩潰您的代碼 - 它是您的XML文件。 Macarse對你的問題有正確的答案,並保持你的代碼相同!

您可能還想看看View Tutorials以瞭解設置XML和使用不同View對象的一些示例。

0

我把XML放進去,但它只顯示一個ImageView下面是我拍攝的模擬器的屏幕截圖。 i852.photobucket.com/albums/ab87/thomasjakway1/Capture.png其值得一提的是,所示的文件是paperrepresentation

如果你看夠硬,你會看到在底部有第二個非常微小的圖像。你只需要增加規模。

相關問題