【问题标题】:How do I insert an image in an activity with android studio?如何使用 android studio 在活动中插入图像?
【发布时间】:2015-01-23 15:44:37
【问题描述】:

我对android 开发非常陌生,并且只阅读并完成了android 开发站点中的第一个指南。我一直遇到的问题是我无法在活动中放置图片。我的drawables文件夹中有图片。我只是不知道如何在屏幕上显示它。任何帮助表示赞赏。

【问题讨论】:

  • 检查this
  • “将图片放入意图中”听起来不对。
  • "我的 drawables 文件夹中有图片。我只是不知道如何在屏幕上显示它" -- 那你为什么认为你需要 "Intent 中的图片“?通过布局文件中的android:srcImageView 上的setImageResource() 将图像放入您的活动中的ImageView

标签: android image android-layout


【解决方案1】:

既然您按照教程进行操作,我想您的屏幕上会显示 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"
/>

【讨论】:

    【解决方案2】:

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


    现在进入activity_main.xml(或您需要添加图像的任何活动)并选择Design 视图。在那里您可以在左侧看到您的调色板工具箱。您需要拖放 ImageView。

    它会提示你Resources对话框。在那里选择项目部分下的Drawable,您可以看到您的图像。如下所示

    选择您想要的图像按确定您可以在设计视图中看到图像。如果您希望它使用 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"
    

    【讨论】:

      【解决方案3】:

      当您将图像放入您的可绘制图库时,您只需选择图像视图选择选项并拖动到您要显示的应用活动中并选择所需的图像。

      【讨论】:

        【解决方案4】:

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

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2020-02-14
          • 2022-01-06
          • 2023-02-07
          • 1970-01-01
          • 1970-01-01
          • 2020-11-28
          • 2019-02-14
          • 1970-01-01
          相关资源
          最近更新 更多