Android:重叠两个视图(故意!)

che*_*ase 11 android android-layout

只是想知道是否有可能重叠两个元素?

这是我想要实现的目标的说明:

在此输入图像描述

基本上它是一个圆形的ImageButton,它的中心位于一个矩形的角落.我应该怎么做定位呢?我可以使用RelativeLayout或其他吗?

dan*_*h32 21

您可以将RelativeLayout用于蓝色框,将ImageView对齐到右上角,然后使用负边距将其推到边界框上.这是一个说明一般想法的示例:

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



    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"        
        android:layout_marginTop="-10dp"
        android:layout_marginRight="-10dp"
        android:src="@drawable/icon"/>
</RelativeLayout>
Run Code Online (Sandbox Code Playgroud)

编辑:我玩了这个更多,你必须在RelativeLayout的父级上设置android:clipChildren ="false".这是一个更完整的样本:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".LoginActivity"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clipChildren="false">
    <RelativeLayout
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#ff0000"
            android:layout_margin="100dp">
        <ImageView
                android:src="@drawable/ic_launcher"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentRight="true"
                android:layout_marginRight="-25dp"
                android:layout_marginTop="-25dp"/>
    </RelativeLayout>
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)