我想在另一个活动之上创建一个透明的活动。

我怎样才能做到这一点呢?


当前回答

2021年的事实

只需添加

<item name="android:windowBackground">@android:color/transparent</item>

你就完成了。

windowIsFloating错误,这使得INSET浮动窗口。

windowcontenttoverlay只与阴影相关。

windowwistranslucent是错误的,它没有使它,所以你可以看到背后的活动。windowIsTranslucent只适用于动画转换。

backgroundDimEnabled使下面的活动变暗,但是,它在不同的设备上是完全错误的。(在某些情况下,除非你使用windowisfloat,否则它什么也不做;总的来说,这种行为是完全错误的/不确定的。)

colorBackgroundCacheHint是无关紧要的,除非在非常旧的设备上,默认是null。

其他回答

为它指定半透明主题

android:theme="@android:style/Theme.Translucent.NoTitleBar"

只要让活动背景图像是透明的。或者在XML文件中添加主题:

<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />

在styles.xml中:

<style name="Theme.AppCompat.Translucent" parent="Theme.AppCompat.NoActionBar">
    <item name="android:background">#33000000</item> <!-- Or any transparency or color you need -->
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>

在AndroidManifest.xml中:

<activity
    android:name=".WhateverNameOfTheActivityIs"
    android:theme="@style/Theme.AppCompat.Translucent">
    ...
</activity>

我想补充一点,因为我也是一个新的Android开发人员。公认的答案很好,但我确实遇到了一些麻烦。我不确定如何在colors.xml文件中添加颜色。下面是应该怎么做:

colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
     <color name="class_zero_background">#7f040000</color>
     <color name="transparent">#00000000</color>
</resources>

在我最初的colors.xml文件中,我有标签“drawable”:

<drawable name="class_zero_background">#7f040000</drawable>

所以我对颜色也这样做了,但我不理解“@color/”引用意味着在XML中寻找标记“color”。我想我也应该提到这一点,以帮助其他人。

如果你正在使用AppCompatActivity,那么在styles.xml中添加这个

<style name="TransparentCompat" parent="@style/Theme.AppCompat.Light.DarkActionBar">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowAnimationStyle">@android:style/Animation</item>
</style>

在清单文件中,您可以像这样将此主题添加到活动标签

android:theme="@style/TransparentCompat"

更多细节请阅读这篇文章