How do I create a transparent Activity on Android?

前端 未结 22 2188
忘掉有多难
忘掉有多难 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:40

    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.

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

    It goes like this:

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

    Along with the gnobal's above solution, I had to set alpha to 0 in the layout file of that particular activity, because on certain phone (Redmi Narzo 20 pro running on Android 10) a dialog portion of the screen was showing with the screen that was supposed to be transparent. For some reason the windowIsFloating was causing this issue, but on removing it I wasn't getting the desired output.

    Steps:

    1. Add the following in the style.xml located under res > values > styles.xml

       <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>
         <item name="android:colorBackgroundCacheHint">@null</item>
       </style>
      

    2. Set the theme of the activity with the above style in AndroidManifest.xml

      <activity
            android:name=".activityName"
            android:theme="@style/Theme.Transparent"/>
      

    3. Open your layout file of the activity on which you applied the above style and set it's alpha value to 0 (android:alpha="0") for the parent layout element.

      <?xml version="1.0" encoding="utf-8"?>
      <androidx.constraintlayout.widget.ConstraintLayout 
          xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:tools="http://schemas.android.com/tools"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:alpha="0">
      
        <WebView        
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:alpha="0"/>
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      

    Please Note: You'll have to extend you activity using Activity() class and not AppCompatActivity for using the above solution.
    0 讨论(0)
  • 2020-11-21 05:43

    In the 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>
    

    In the AndroidManifest.xml:

    <activity
        android:name=".WhateverNameOfTheActivityIs"
        android:theme="@style/Theme.AppCompat.Translucent">
        ...
    </activity>
    
    0 讨论(0)
提交回复
热议问题