【问题标题】:How to draw 2 PNG Images to the screen at the same time如何同时在屏幕上绘制 2 个 PNG 图像
【发布时间】:2011-03-15 01:22:52
【问题描述】:

我想知道如何在屏幕上绘制两张 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); 会使我的应用程序在启动时崩溃。

【问题讨论】:

    标签: android image png draw imageview


    【解决方案1】:

    将 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

    【讨论】:

    • 我把 XML 放进去,但它只显示一个 ImageView 这是我拿的模拟器的截图。 i852.photobucket.com/albums/ab87/thomasjakway1/Capture.png值得一提的是显示的文件是paperrepresentation
    • 那是因为尺寸。尝试使用layout_widthlayout_height 使用类似“40dp”的东西。还要记住,LinearLayout 设置为 Vertical。
    【解决方案2】:

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

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

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2023-03-07
        • 2011-07-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-25
        相关资源
        最近更新 更多