Android: 将一张图片放在另一张图片上方

9
我有三个 ImageView,我希望其中一个 ImageView 在背景中,而另外两个 ImageView 则位于其上。我不能使用其他视图,因为该图像以图片形式下载,无法设置为 Linear 或 Relative 布局的背景。
第一张图片: enter image description here 这是我实际想要的第二张图片: enter image description here

使用FrameLayout来满足您的需求。 - RajaReddy PolamReddy
6个回答

13

你可以按照以下的xml布局尝试...

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" >

    <ImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/ic_launcher"
        android:scaleType="fitXY" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_marginBottom="100dip"
        android:background="@drawable/ic_launcher" />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom|center_horizontal"
        android:layout_marginBottom="100dip"
        android:background="@drawable/ic_launcher" />

</FrameLayout>

1
你可以像这样使用:
<FrameLayout 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" >

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

</FrameLayout>

1

为了重叠视图或者封锁单个项目的显示区域,可以使用Frame布局(在这里)。

你可以使用:

     <FrameLayout 
        android:layout_width="..."
        android:layout_height="..."
        android:background="Image1"
        <ImageView
            android:layout_width="..."
            android:layout_height="..."
            android:background="image2">
         <ImageView
            android:layout_width="..."
            android:layout_height="..."
            android:background="image3">   
     </FrameLayout>

1

这是我发现的合并多张图片的最简单方法。如果我们想分享合并后的文件,这非常有帮助。

Resources r = getResources();    
Drawable[] layers = new Drawable[3];
layers[0] = r.getDrawable(R.drawable.image3);
layers[1] = r.getDrawable(R.drawable.image1);
layers[2] = r.getDrawable(R.drawable.image2);

LayerDrawable layerDrawable= new LayerDrawable(layers);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
// align image1 over image3
layerDrawable.setLayerGravity(1, Gravity.TOP);
layerDrawable.setLayerInsetTop(1, 250);

// align image2 over image3
layerDrawable.setLayerGravity(2, Gravity.BOTTOM);
layerDrawable.setLayerInsetBottom(2, 250);
}
 // both image1 & 2 placed over image3 in layerDrawable.
imageView.setImageDrawable(layerDrawable);

希望这个能够工作。

0
您可以使用 layer-list 在 drawable 中创建 layers.xml,就像这样:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item>
    <bitmap android:src="@drawable/img1"
        android:gravity="center"
        />
</item>
<item>
    <bitmap android:src="@drawable/img2"
        android:gravity="center"
        />
</item>
</layer-list>

0

首先在RelativeLayout中添加一个ImageView,将其宽度和高度设置为match_parent。然后对于第二个ImageView,将alignParentTop属性设置为true,对于第三个ImageView也同样设置其alignParentBottom属性为true,同时将其宽度和高度设置为wrap_content。


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