如何在 Android 上创建透明的活动?

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

我该如何实现?

答案

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>
<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
...
</activity>
<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<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>
<activity
    android:name=".WhateverNameOfTheActivityIs"
    android:theme="@style/Theme.AppCompat.Translucent">
    ...
</activity>
<activity   
     android:name=".yourActivity"    
     android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>
<activity
    android:name="YOUR COMPLETE ACTIVITY NAME WITH PACKAGE"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />
<?xml version="1.0" encoding="utf-8"?>
<resources>
     <color name="class_zero_background">#7f040000</color>
     <color name="transparent">#00000000</color>
</resources>
<drawable name="class_zero_background">#7f040000</drawable>

我通过在清单中的活动标签中添加android:theme="@android:style/Theme.Translucent"在 2.3.3 上实现了此目标。

我不知道较低的版本...

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Transparent" parent="android:Theme">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>
@Override
    protected void onCreate(Bundle savedInstanceState) {

        String email = getIntent().getStringExtra(AppConstants.REGISTER_EMAIL_INTENT_KEY);
        if (email != null && !email.isEmpty())
        {
            // We have the valid email ID, no need to take it from user, prepare transparent activity just to perform bg tasks required for login
            setTheme(R.style.Theme_Transparent);
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);

        }
        else
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_dummy);


    }
<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));