【问题标题】:How do I create a transparent Activity on Android?如何在 Android 上创建一个透明的 Activity?
【发布时间】:2011-01-11 17:25:10
【问题描述】:

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

我怎样才能做到这一点?

【问题讨论】:

  • 你能告诉我们透明活动有什么用

标签: android android-activity transparent


【解决方案1】:

在您的res/values/styles.xml 文件中添加以下样式(如果您没有,请创建它。)这是一个完整的文件:

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

@color/transparent的值是我放在res/values/color.xml文件中的颜色值#00000000,你也可以在以后的Android版本中使用@android:color/transparent。)

然后将样式应用到您的活动中,例如:

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

【讨论】:

  • 我用&lt;item name="android:windowBackground"&gt;@android:color/transparent&lt;/item&gt;
  • 太棒了!只有一项改进:如果您使用 parent="@android:style/Theme.Dialog" 您将获得对话框的确切行为。这意味着淡入/淡出而不是滑入/滑出(就像一个活动)
  • 正如@Emilio 提到的,这将表现得像一个对话框,主要是因为android:windowIsFloating 设置为true。删除此属性以使其表现得像正常活动(在这种情况下,它将匹配 android:style/Theme.Translucent.NoTitleBar
  • 我删除了 true 以获得全屏透明活动
  • 我的活动来自AppCompatActivity。所以parent="android:Theme" 让我的应用程序崩溃了。我刚刚删除了它,它就像魅力一样。谢谢!
【解决方案2】:

是这样的:

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

【讨论】:

  • 您只需将 alex 发布的主题添加到清单中的活动声明中 - 这个位 android:theme="@android:style/Theme.Translucent.NoTitleBar,您可以为每个活动分配它半透明主题
  • 你能告诉我如何将活动透明化 50% 吗?因为这是 100%,我需要 50%
  • @user1129443:50% black should be #7f000000。每个组件(A、R、G、B)都可以从0-255 中获取值。 50% of 255 = 127. 127 in Hex = 7F那如何计算透明度(opacity)
  • 这种方法在 Activity 运行时会锁定 UI,但由于它设置为半透明,因此无法做任何事情。有没有办法避免这种 UI 锁定。
  • @yanchenko 不幸的是,这不是一个好的解决方案。正如 Akhil 所说,它锁定了 UI!
【解决方案3】:

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

【讨论】:

  • 如果您打算在此 Activity 上实际显示某些内容(例如 Dialog 或 DialogFragment),您会注意到所有内容都是 Dark 主题。因此,您可能希望您的主题继承自 Theme.Appcompat.Light.NoActionBar
  • 在我的情况下,它显示黑色背景。我有父主题设置了其他内容,但在一项特定活动中,我正在更改上述主题。有帮助吗?
  • 当我删除“android:background”时效果很好
  • 我认为你想删除background 并将你喜欢的半透明颜色放入windowBackground
  • 如果您的活动使用 AppCompatActivity 与@gnobal 的答案相比,这应该是答案。
【解决方案4】:

像这样在清单中声明您的活动:

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

并为您的布局添加透明背景。

【讨论】:

  • 最佳方式。谢谢
  • 您需要在此活动中使用 Theme.AppCompat 主题(或后代)。
【解决方案5】:

在项目的 Android 清单文件中将半透明主题分配给要使其透明的 Activity:

<activity
    android:name="YOUR COMPLETE ACTIVITY NAME WITH PACKAGE"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

【讨论】:

    【解决方案6】:

    就我而言,我必须根据某些条件在 java 中的运行时设置主题。所以我创建了一个风格主题(类似于其他答案):

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

    然后在 Java 中我将它应用到我的活动中:

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

    请记住这里的一个要点:您必须在super.onCreate(savedInstanceState); 之前调用setTheme() 函数。我错过了这一点,卡了 2 个小时,想为什么我的主题没有在运行时反映。

    【讨论】:

      【解决方案7】:

      我想补充一点,因为我也是一名新的 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 中查找标记“颜色”。我认为我也应该提及这一点以帮助其他人。

      【讨论】:

        【解决方案8】:

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

        我不知道低版本...

        【讨论】:

        • 这也适用于 2.2。我刚刚创建了一个带有列表视图的简单活动,它漂浮在最后一个活动之上。
        • 它是在 API 1 中添加的,这不是问题 :)
        • 如果你正在使用这个,不要使用AppCompatActivity
        • 在 7.0 中也可以使用,因此它是一种很好的方法。我修改为@android:style/Theme.Translucent.NoTitleBar.Fullscreen
        【解决方案9】:

        2021 年事实

        只需添加

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

        你已经完成了。

        windowIsFloating 错误,这会生成一个 INSET 浮动窗口。

        windowContentOverlay 仅与阴影有关。

        windowIsTranslucent 是错误的,它没有成功,因此您可以看到背后的活动。 windowIsTranslucent 仅在动画过渡时才相关。

        backgroundDimEnabled 使下面的活动变暗,但是,它在不同的设备上完全是错误的。 (在某些情况下,除非您使用 windowIsFloating,否则它什么也不做;一般来说,这种行为完全是错误的/不确定的。)

        colorBackgroundCacheHint 无关紧要,除非在非常旧的设备上,反正默认为 null。

        【讨论】:

        • “非常老旧的设备” - 写于 10 年前,距离 Android 推出仅 2 年。我想 2011 年到 2021 年的设备必须被认为是“古老的”。大声笑
        • 您的解决方案实际上并没有做到这一点。公认的答案,它已有 10 年历史,但它可以找到.. 只需要扩展 AppCompat 主题而不是 Theme..
        • 接受的答案是完全错误的,原因在此处的五个要点中进行了解释。 (您可以简单地阅读每个点的文档以了解事实。)
        【解决方案10】:

        onCreate 函数中,在 setContentView 下方,添加以下行:

        getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        

        【讨论】:

        • 由于某种原因使背景完全变黑。
        • 我的也是@SubinSebastian,有没有人找到解决办法?
        【解决方案11】:

        只要让活动背景图片透明即可。或者在 XML 文件中添加主题:

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

        【讨论】:

          【解决方案12】:

          我发现的最简单的方法是将AndroidManifest 中的活动主题设置为android:theme="@android:style/Theme.Holo.Dialog"

          然后在activity的onCreate方法中,调用getWindow().setBackgroundDrawable(new ColorDrawable(0));

          【讨论】:

            【解决方案13】:

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

            getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
            

            但您还需要将活动中的主视图设置为不可见。否则背景将不可见,而其中的所有视图都将可见。

            【讨论】:

            • 使背景完全变黑
            【解决方案14】:

            除了以上答案:

            为了避免 android Oreo 相关的活动崩溃

            <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" />
            

            【讨论】:

            • 截至 2018 年,这应该是最佳答案
            • 使用 API 28 在模拟器上给了我一个黑色背景
            • 我试过这个来修复与在 android 8.0 中设置方向相关的崩溃,但我仍然收到 IllegalStateException: Only fullscreen opaque activities requestorientation
            • 效果很好。在 Android 11 上的三星 S20 上进行了测试。它仍然在模拟器上显示黑屏,但在其他情况下只是屏幕略微变暗。
            • 添加下面的代码应该可以消除作为对话框主题的暗淡。 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
            【解决方案15】:

            如果您使用的是 AppCompatActivity,则将其添加到 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>
            

            manifest 文件中,您可以像这样将此主题添加到活动标签中

            android:theme="@style/TransparentCompat"
            

            更多详情请阅读article

            【讨论】:

            【解决方案16】:

            我只做了两件事,它使我的活动变得透明。它们在下面。

            1. 在清单文件中,我刚刚在 activity 标记中添加了以下代码。

              android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
              
            2. 我只是将该活动的主布局的背景设置为“#80000000”。喜欢

              android:background="#80000000"
              

            它非常适合我。

            【讨论】:

              【解决方案17】:

              为其指定半透明主题

              android:theme="@android:style/Theme.Translucent.NoTitleBar"
              

              【讨论】:

                【解决方案18】:

                有两种方法:

                1. 使用 Theme.NoDisplay
                2. 使用 Theme.Translucent.NoTitleBar

                使用Theme.NoDisplay 仍然可以使用……但只能在较旧的 Android 设备上使用。在 Android 6.0 及更高版本上,使用 Theme.NoDisplay 而不在 onCreate() (or, technically, before onResume()) 中调用 finish()崩溃您的应用。这就是为什么推荐是使用Theme.Translucent.NoTitleBar,它不受这个限制的影响。”

                【讨论】:

                  【解决方案19】:

                  注意1:在Drawable文件夹中创建test.xml并复制以下代码

                     <?xml version="1.0" encoding="UTF-8"?>
                  <shape xmlns:android="http://schemas.android.com/apk/res/android"
                      android:shape="rectangle" >
                  
                      <stroke android:width="2dp" />
                  
                      <gradient
                          android:angle="90"
                          android:endColor="#29000000"
                          android:startColor="#29000000" />
                  
                      <corners
                          android:bottomLeftRadius="7dp"
                          android:bottomRightRadius="7dp"
                          android:topLeftRadius="7dp"
                          android:topRightRadius="7dp" />
                  
                  </shape>
                  

                  // 注意:角和形状根据您的要求。

                  // 注2:创建xml:

                      <?xml version="1.0" encoding="utf-8"?>
                      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                          android:layout_width="match_parent"
                          android:layout_height="match_parent"
                          android:background="@drawable/test"
                          android:orientation="vertical" >
                  
                          <LinearLayout
                              android:layout_width="fill_parent"
                              android:layout_height="wrap_content"
                              android:layout_weight="1.09"
                              android:gravity="center"
                           android:background="@drawable/transperent_shape"
                              android:orientation="vertical" >
                       </LinearLayout>
                      </LinearLayout>
                  

                  【讨论】:

                    【解决方案20】:

                    只需将以下行添加到清单文件中需要看起来透明的活动标记中。

                    android:theme="@android:style/Theme.Translucent"
                    

                    【讨论】:

                      【解决方案21】:

                      所有这些答案都可能令人困惑,透明活动和无 UI 活动之间存在差异。

                      使用这个:

                      android:theme="@android:style/Theme.Translucent.NoTitleBar"

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

                      如果你想要一个无 UI 活动而不是使用这个:

                      android:theme="@android:style/Theme.NoDisplay"

                      【讨论】:

                        【解决方案22】:

                        您可以从活动中删除setContentView(R.layout.mLayout),并将主题设置为android:theme="@style/AppTheme.Transparent"。查看此link 了解更多详情。

                        【讨论】:

                          【解决方案23】:

                          把它放在 style.xml 中

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

                          或添加清单

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

                          【讨论】:

                            【解决方案24】:

                            连同上述gnobal's 解决方案,我必须在该特定活动的布局文件中将 alpha 设置为 0,因为在某些手机上(运行 Android 10 的 Redmi Narzo 20 pro)屏幕的对话框部分与应该是透明的屏幕一起显示。出于某种原因,windowIsFloating 导致了这个问题,但是在删除它时我没有得到想要的输出。

                            步骤:

                            1. 在 res > values > styles.xml 下的 style.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. 用上面的样式设置activity的主题在 AndroidManifest.xml

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

                            3. 打开应用了上述样式的 Activity 的布局文件,并将父布局元素的 alpha 值设置为 0 (android:alpha="0")。

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

                            请注意:您必须使用 Activity() 类而不是 AppCompatActivity 来扩展您的活动,才能使用上述解决方案。

                            【讨论】:

                              猜你喜欢
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 1970-01-01
                              • 2016-12-02
                              相关资源
                              最近更新 更多