在 Android 中添加背景图片的方法有以下几种:
1. 在 XML 布局文件中设置:
```xml
android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/your_image" /> ``` 2. 在 Java 代码中设置: ```java View view = findViewById(R.id.your_view); view.setBackgroundResource(R.drawable.your_image); ``` 3. 在 XML 布局文件中设置 `android:background` 属性: ```xml android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/your_image" /> ``` 4. 使用 `setBackgroundDrawable()` 方法设置背景图片: ```java View view = findViewById(R.id.your_view); Drawable drawable = ContextCompat.getDrawable(this, R.drawable.your_image); view.setBackgroundDrawable(drawable); ``` 注意事项: 1. `your_image` 是你的背景图片在 `res/drawable` 目录下的文件名。 2. 如果你需要设置整个 `Activity` 或 `Fragment` 的背景图片,可以在 `activity_main.xml` 或 `fragment_your_fragment.xml` 文件中设置 `android:background` 属性。 3. 如果你需要动态设置背景图片,可以使用 `setBackgroundResource()` 或 `setBackgroundDrawable()` 方法。 4. 确保你的图片资源文件的大小和质量适当,否则可能会影响应用程序的性能。