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

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


当前回答

除了以上回答:

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

<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" />

其他回答

在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>

在manifest中像这样声明你的活动:

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

并在布局中添加透明背景。

你可以从你的活动中删除setContentView(r.b ayout. mlayout),并设置主题为android:theme="@style/AppTheme.Transparent"。查看这个链接了解更多细节。

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

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

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

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似乎已经解决了这个问题