How do I create a transparent Activity on Android?

前端 未结 22 2121
忘掉有多难
忘掉有多难 2020-11-21 04:43

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

How can I achieve this?

相关标签:
22条回答
  • 2020-11-21 05:16

    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" />
    
    0 讨论(0)
  • 2020-11-21 05:21

    Just let the activity background image be transparent. Or add the theme in the XML file:

    <activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
    
    0 讨论(0)
  • 2020-11-21 05:23

    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.”

    0 讨论(0)
  • 2020-11-21 05:24

    in addition to the above answers:

    to avoid android Oreo related crash on activity

    <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" />
    
    0 讨论(0)
  • 2020-11-21 05:24

    If you are using AppCompatActivity then add this in styles.xml

    <style name="TransparentCompat" parent="@style/Theme.AppCompat.Light.DarkActionBar">
        <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>
    

    In manifest file you can add this theme to activity tag like this

    android:theme="@style/TransparentCompat"
    

    for more details read this article

    0 讨论(0)
  • 2020-11-21 05:25

    Assign it the Translucent theme

    android:theme="@android:style/Theme.Translucent.NoTitleBar"
    
    0 讨论(0)
提交回复
热议问题