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

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


当前回答

我想补充一点,因为我也是一个新的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”。我想我也应该提到这一点,以帮助其他人。

其他回答

把它放在style.xml中

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

或在清单中添加

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

它是这样的:

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

所有这些答案可能会让人困惑,透明活动和无UI活动之间是有区别的。

用这个:

android:主题= " @android:风格/ Theme.Translucent.NoTitleBar”

将使活动透明,但将阻塞UI。

如果你想要一个None的UI活动,请使用这个:

android:主题= " @android:风格/主题。NoDisplay”

对于对话活动,我使用这个:

getWindow().getDecorView().setBackgroundResource(android.R.color.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>