首页 > 解决方案 > 在 Android Studio 中创建叠加层

问题描述

我目前正在制作一个 Android 应用程序,我正在从下面的图片中寻找灵感。我正在尝试在图片中第一个设备的右上角创建叠加层,但我不知道它是如何完成的。 在此处输入图像描述

标签: androidxmlandroid-layout

解决方案


使用alpha属性

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
<LinearLayout
    android:background="#FFFF33"
    android:layout_width="match_parent"
    android:layout_height="200dp">
    <TextView
        android:textAlignment="textEnd"
        android:text="Sample Text View Sample Text View Sample Text View"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

    <RelativeLayout
        android:alpha="0.5"
        android:background="@color/colorPrimary"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_width="100dp"
        android:layout_height="200dp">
    </RelativeLayout>
</RelativeLayout>


</RelativeLayout>

输出将像

在此处输入图像描述

*********** EDITED AS PER OP'S REQUIREMENT***********

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:background="#FFFF33"
        android:layout_width="match_parent"
        android:layout_height="200dp">

    </LinearLayout>

    <RelativeLayout
        android:alpha="0.5"
        android:background="@color/colorPrimary"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:layout_width="100dp"
        android:layout_height="200dp">
    </RelativeLayout>
    <TextView
        android:textAlignment="textEnd"
        android:text="Sample Text View Sample Text View Sample Text View"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>
</RelativeLayout>

推荐阅读