How do I create a transparent Activity on Android?

2018-12-31 00:29发布

I want to create a transparent Activity on top of another activity.

How can I achieve this?

19条回答
栀子花@的思念
2楼-- · 2018-12-31 00:38

I just did two things, and it made my activity transparent. They are below.

  1. In the manifest file I just added the below code in the activity tag.

    android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
    
  2. And I just set the background of the main layout for that activity as "#80000000". Like

    android:background="#80000000"
    

It perfectly works for me.

查看更多
裙下三千臣
3楼-- · 2018-12-31 00:42

In my case, i have to set the theme on the runtime in java based on some conditions. So I created one theme in style (similar to other answers):

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

Then in Java I applied it to my activity:

@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);


    }

Remember one Important point here: You must call the setTheme() function before super.onCreate(savedInstanceState);. I missed this point and stucked for 2 hours, thinking why my theme is not reflected at run time.

查看更多
几人难应
4楼-- · 2018-12-31 00:43

Assign the translucent theme to the activity that you want to make transparent in the Android manifest file of your project:

<activity
    android:name="YOUR COMPLETE ACTIVITY NAME WITH PACKAGE"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />
查看更多
明月照影归
5楼-- · 2018-12-31 00:45

In the onCreate function, below the setContentView, add this line:

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
查看更多
时光乱了年华
6楼-- · 2018-12-31 00:46

The easiest way that I have found is to set the activity's theme in the AndroidManifest to android:theme="@android:style/Theme.Holo.Dialog".

Then in the activity's onCreate method, call getWindow().setBackgroundDrawable(new ColorDrawable(0));.

查看更多
琉璃瓶的回忆
7楼-- · 2018-12-31 00:47

There're two ways:

  1. Using Theme.NoDisplay
  2. Using Theme.Translucent.NoTitleBar

Using Theme.NoDisplay will still work… but only on older Android devices. On Android 6.0 and higher, using Theme.NoDisplay without calling finish() in onCreate() (or, technically, before onResume()) will crash your app. This is why the recommendation is to use Theme.Translucent.NoTitleBar, which does not suffer from this limitation.”

查看更多
登录 后发表回答