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

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


当前回答

其他解决方案对我有用,但我注意到一个问题,可能会影响一些人。当我像这样开始我的活动时:

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

而不是像这样的一行字

startActivity(new Intent(this, MainActivity.class));

我只得到黑色的背景,这很痛苦。我还没有弄清楚是什么导致了这个,我猜这是设备相关的(在小米上测试)。传递值也需要像这样。

startActivity(new Intent(this, MainActivity.class)
                            .putExtra("SOME_VALUE", value)
                            .putExtra("ANOTHER_VALUE", value2));

编辑: 将父样式更改为Theme.AppCompat.Light.NoActionBar似乎已经解决了这个问题

其他回答

我只做了两件事,它让我的活动变得透明。它们在下面。

在清单文件中,我只是在活动标记中添加了以下代码。 android:主题= " @android:风格/ Theme.Translucent.NoTitleBar.Fullscreen” 然后我将该活动的主布局的背景设置为“#80000000”。就像 android:背景= " # 80000000 "

它非常适合我。

我发现的最简单的方法是在AndroidManifest中设置活动的主题为android:theme="@android:style/ theme . holo . dialog "。

然后在活动的onCreate方法中,调用getWindow()。setBackgroundDrawable(新ColorDrawable(0));。

把它放在style.xml中

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

或在清单中添加

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

在onCreate函数中,在setContentView的下面,添加这一行:

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

除了以上回答:

以避免安卓奥利奥相关的崩溃活动

<style name="AppTheme.Transparent" parent="@style/Theme.AppCompat.Dialog">
    <item name="windowNoTitle">true</item>
    <item name="android:windowCloseOnTouchOutside">false</item>
</style>

<activity
     android:name="xActivity"
     android:theme="@style/AppTheme.Transparent" />