Android相对布局图像叠加

时间:2014-03-15 09:42:35

标签: android image imageview relativelayout

enter image description here

我有2张图片,我想要这样的节目。第二张图像应该是第一张图像的右上角。底部或顶部并不重要。

我正在尝试相对布局,但看起来不像这样

我希望首先使用此

将取消图像放到图像的底部
android:layout_alignParentBottom="true"

但屏幕底部不是图像底部。

4 个答案:

答案 0 :(得分:2)

您也可以将Frame Layout用于此目的,因为框架布局不需要任何关系,例如android:layout_gravity="top|right"属性:

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

    <ImageView
        android:id="@+id/image1"
        android:layout_width="20dp"
        android:layout_height="20dp"
        android:layout_gravity="top|right"

        android:src="@drawable/ic_launcher" >
    </ImageView>

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dip"
        android:layout_below="@id/image1"
        android:src="@drawable/ic_launcher" >
    </ImageView>

</FrameLayout>

答案 1 :(得分:0)

AlignParentBottom只会将图像设置到页面底部,您需要的是将图像对齐(X)图像,并且(X)图像在父图像中对齐。只需更改下面粘贴代码的src,看看是否

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

        <ImageView
            android:id="@+id/image1"
            android:layout_width="20dp"
            android:layout_height="20dp"
            android:layout_alignParentRight="true"
            android:src="@drawable/ic_launcher" >
        </ImageView>

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_below="@id/image1"
            android:src="@drawable/ic_launcher" >
        </ImageView>

    </RelativeLayout>

答案 2 :(得分:0)

在代码下面使用它肯定会帮助你。

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">

<FrameLayout   
    android:layout_width="210dp"
    android:layout_height="210dp"
    >

    <ImageView
            android:layout_width="190dp"
            android:layout_height="180dp"
            android:layout_gravity="bottom"
            android:background="@drawable/red_button"
           >
    </ImageView>
        <ImageView
            android:id="@+id/image1"
            android:layout_width="40dp"
            android:layout_height="40dp"
            android:layout_gravity="right"
            android:background="@drawable/icon" >
        </ImageView>
    </FrameLayout>

</RelativeLayout>

由于

答案 3 :(得分:0)

在清单中制作一个透明主题的活动,如下所示:

<activity android:name=".MyDialogActivity" android:theme="@style/Theme.Transparent" />

然后定义这样的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent" android:layout_height="fill_parent">
    <RelativeLayout android:orientation="vertical"
        android:layout_width="fill_parent" android:layout_height="fill_parent"
        android:layout_margin="15dp" android:padding="2dp">
        <!-- Contents will go here..-->
    </RelativeLayout>
    <Button android:layout_alignParentRight="true" android:text="X"
        android:textColor="#FFF" android:background="@drawable/round_button_background"
        android:gravity="center_vertical|center_horizontal"
        android:layout_margin="5dp" android:layout_height="25dp"
        android:layout_width="25dp" android:textSize="12sp" android:textStyle="bold"
        android:onClick="cancelActivity" />
</RelativeLayout>

并在drawable中定义关闭Button的背景,如下所示:

round_button_background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
        android:shape="oval">
    <solid android:color="#9F2200" />
    <stroke android:width="2dp" android:color="#FFF" />
</shape>
相关问题