Android 怎么插入图片
时间: 2023-11-03 22:18:53 浏览: 39
要在 Android 中插入图片,你可以使用 ImageView 控件。首先,在 XML 布局文件中添加 ImageView 控件,并设置它的布局参数和属性,例如:
```
<ImageView
android:id="@+id/my_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/my_image_file" />
```
其中,@drawable/my_image_file 表示要显示的图片文件名,这个文件需要放在 res/drawable 目录下。
然后,在 Java 代码中获取 ImageView 控件的引用,并设置它的图片资源,例如:
```
ImageView myImage = findViewById(R.id.my_image);
myImage.setImageResource(R.drawable.my_image_file);
```
其中,R.drawable.my_image_file 是在资源文件中自动生成的 ID,它对应的就是你在 XML 中设置的图片文件。
这样,就可以在 Android 应用中插入图片了。