如何使用 android studio 在 activity 中插入图像?

How do I insert an image in an activity with android studio?

我是 android 开发的新手,只阅读并完成了 android 开发站点中的第一篇指南。我一直遇到的问题是我无法将图片放入 activity。我的可绘制文件夹中有图片。我只是不知道如何将它显示在屏幕上。感谢任何帮助。

自从您学习了本教程后,我想您的屏幕显示 Hello World。

这意味着您的布局中有一些代码 xml 看起来像这样

<TextView        
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

您想要显示图像,因此您想要 ImageView 而不是 TextView。而不是文本属性你想要一个 src 属性,它链接到你的可绘制资源

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/cool_pic"
/>

我将解释如何使用 Android studio(2.3.3) 添加图像。首先,您需要将图像添加到项目中的 res/drawable 文件夹中。喜欢下面


现在转到 activity_main.xml(或您需要添加图像的任何 activity)和 select Design 视图。您可以在左侧看到您的调色板工具箱。您需要拖放 ImageView。

它会提示你资源对话框。在项目部分的 select Drawable 中,您可以看到您的图像。喜欢下面

Select你想要的图片按确定你可以在设计视图中看到图片。如果你想使用 xml 配置它,它看起来像下面这样。

<ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/homepage"
        tools:layout_editor_absoluteX="55dp"
        tools:layout_editor_absoluteY="130dp" />

您需要使用

提供图片位置
app:srcCompat="@drawable/imagename"

当您将图片添加到您的可绘制图库中时,您只需选择图片视图选项,然后将其拖入您要显示的应用 activity 和 select 所需的图片。

复制要在 android 应用程序中显示的图像并粘贴到可绘制文件夹中。给出下面的代码

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:src="@drawable/image"
 />