Android中怎么将LinearLayout.xml中一个图片放在另一个图片上面
时间: 2024-10-08 13:10:03 浏览: 61
在Android中,如果你想在一个`LinearLayout`布局文件中将一张图片放在另一张图片上面,你可以使用`RelativeLayout`或者`FrameLayout`作为包含这两个图片的容器,因为它们支持元素之间的直接相对定位。
1. **使用`RelativeLayout`**:
首先,在XML布局文件中,将`LinearLayout`替换为`RelativeLayout`。然后添加两个`ImageView`:
```xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- 定义第一个ImageView -->
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<!-- 定义第二个ImageView -->
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image2"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true" />
</RelativeLayout>
```
2. **使用`FrameLayout`**:
如果你需要图片完全覆盖,可以使用`FrameLayout`,让其中一个图片设置为`android:layout_gravity="top"`:
```xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image1"
android:layout_gravity="center_horizontal|top" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/image2"
android:layout_gravity="center_horizontal" />
</FrameLayout>
```
阅读全文
相关推荐

















