在另一张图片上方的图像

3
我有2张图片,想把其中一张图片放在另一张图片上面。我的XML代码如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<ImageView
    android:id="@+id/imgThumb"
    android:layout_width="60dip"
    android:layout_height="80dip"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20sp"
    android:src="@drawable/bg" />

<TextView
    android:id="@+id/imgText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dip"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="test string"
    android:textColor="#000000"
    android:textSize="10dip"
    android:visibility="gone" />

<View
    android:layout_width="fill_parent"
    android:layout_height="30sp"
    android:background="@drawable/shelf" />

布局如下:

enter image description here

我希望第一张图片覆盖在第二张图片之上,让第一张图片看起来像是站在第二张图片上方。


请使用相对布局(RelativeLayout)+帧布局(FrameLayout)或仅使用RelativeLayout。 - Kosh
3个回答

2
把布局从线性布局更改为相对布局或框架布局,并在视图中添加顶部边距。 在相对布局中,每个控件都会自动排列在其上方的控件之上。 请尝试以下代码。
<ImageView
    android:id="@+id/imgThumb"
    android:layout_width="60dip"
    android:layout_height="80dip"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20sp"
    android:src="@drawable/abc" />

<TextView
    android:id="@+id/imgText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="5dip"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="test string"
    android:textColor="#000000"
    android:textSize="10dip"
    android:visibility="gone" />

<View
    android:layout_width="fill_parent"
    android:layout_height="30sp"
    android:layout_marginTop="20sp"
    android:background="@drawable/ic_launcher" />


2

将LinearLayout改为RelativeLayout,交换两个图片的位置。

<?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="match_parent" >

<View
    android:id="@+id/shelf"
    android:layout_below="@+id/imgThumb"
    android:layout_width="fill_parent"
    android:layout_height="30sp"
    android:background="@drawable/shelf" />

<ImageView
    android:id="@+id/imgThumb"
    android:alignParentTop="true"
    android:layout_width="60dip"
    android:layout_height="80dip"
    android:layout_gravity="center_horizontal"
    android:layout_marginTop="20sp"
    android:src="@drawable/bg" />

2
我不知道为什么有人给我点了踩,我是新来的,所以才问这个问题。 - A J

1
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >

    <ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginLeft="138dp"
    android:layout_marginTop="142dp"
    android:src="@drawable/ic_launcher" />

    <ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/imageView1"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="16dp"
    android:src="@drawable/ic_launcher" />

    <ImageView
    android:id="@+id/imageView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView2"
    android:layout_alignTop="@+id/imageView2"
    android:layout_marginTop="15dp"
    android:src="@drawable/ic_launcher" />

    </RelativeLayout>

enter image description here


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接