12

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

这是我试图实现的目标的说明:

在此处输入图像描述

基本上它是一个圆形 ImageButton,其中心位于矩形的角上。我应该如何定位它?我可以使用 RelativeLayout 或其他东西吗?

4

3 回答 3

24

您可以为蓝色框使用 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>

编辑:我玩得更多了,你必须在 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>
于 2013-06-27T19:25:41.130 回答
3

一个非常简单的解决方案是将 ImageView 的边距设为负值(例如 -40dp)。但它只在某些情况下有效。

于 2015-01-15T11:17:35.627 回答
0

另一种简单的方法是使用两个透明图像视图。这些图像视图将位于蓝色布局的顶部和右侧。将此图像视图的背景设置为 #00000000 以使其透明。

于 2015-10-19T14:51:06.333 回答