android - Android 中的 float 透明窗口

标签 android view transparent android-toast

我正在将我的 iOS 应用移植到 Android。它是一个音乐播放器,我有一个窗口显示在应用程序主屏幕的顶部,该窗口逐渐消失 - 类似于 Toast,让用户了解有关轨道的信息。

这个窗口的特别之处在于它是半透明的。它允许用户点击它后面的按钮,所以可以说它不是一个对话框。

我如何在 Android 中实现这样的窗口?假设我有一个带有许多按钮的 MainActivity,并且我想生成这种特殊类型的窗口。它只是一个不可聚焦的 View 吗?还是我应该尝试创建自定义 Toast?

最佳答案

没有可以让您触摸下方的组件,但您可以在 RelativeLayout 中创建一个布局,它支持 z 轴上的多种布局,这样您就可以触摸下方的按钮。

您可以试用此示例并测试:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/parent"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@android:color/white"
    android:clipChildren="true" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/countdown"
        android:layout_marginBottom="24dp"
        android:layout_toRightOf="@+id/countdown"
        android:text="Button" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button1"
        android:layout_below="@+id/button1"
        android:layout_marginLeft="43dp"
        android:layout_marginTop="97dp"
        android:text="Button" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_toLeftOf="@+id/button2"
        android:text="Button" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button3"
        android:layout_alignParentBottom="true"
        android:layout_marginBottom="99dp"
        android:text="Button" />

    <Button
        android:id="@+id/button9"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button7"
        android:layout_marginBottom="15dp"
        android:layout_marginLeft="10dp"
        android:layout_toRightOf="@+id/button8"
        android:text="Button" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button4"
        android:layout_alignBottom="@+id/button4"
        android:layout_toLeftOf="@+id/button9"
        android:text="Button" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/button1"
        android:layout_marginLeft="12dp"
        android:layout_marginTop="13dp"
        android:text="Button" />

    <Button
        android:id="@+id/button8"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button7"
        android:layout_alignTop="@+id/button2"
        android:layout_marginLeft="41dp"
        android:layout_marginTop="25dp"
        android:text="Button" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/button6"
        android:layout_marginBottom="12dp"
        android:layout_marginLeft="16dp"
        android:layout_toRightOf="@+id/button2"
        android:text="Button" />

    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="400dp"
        android:layout_centerInParent="true"
        android:background="#96000000"
        android:orientation="vertical" >
    </LinearLayout>

</RelativeLayout>

那个结果:

enter image description here

在该图像中,黑色半透明布局是位于按钮顶部的布局,您可以将其用作半透明对话框。您也可以单击它下面的按钮。

关于android - Android 中的 float 透明窗口,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25298420/

相关文章:

Android:使 Dialog 周围的一切都比默认更暗

Android 使用 View 模型和实时数据实现搜索

android - SQLiteDatabase 使用路径打开特定数据库

android - 替换 MapView OnLongPress 监听器

sql-server - 无法使用 SQL Server 中的用户定义函数在 View 上创建索引

mvvm - Prism 登录屏幕

swift - 如何快速编辑背景图像的不透明度

android - Flex 移动应用程序不适用于 android 2.2

tsql - 创建可以处理不同表的存储过程

ssl - Squid:透明 SSL 代理的总线错误