979

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

我怎样才能做到这一点?

4

24 回答 24

1460

在您的文件中添加以下样式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的颜色值。你也可以在以后的Android版本中使用。)#00000000res/values/color.xml@android:color/transparent

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

<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
...
</activity>
于 2010-04-23T17:37:26.347 回答
200

它是这样的:

<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
于 2010-02-01T14:47:54.487 回答
143

在样式.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>
于 2015-07-28T21:26:38.920 回答
39

在清单中声明您的活动,如下所示:

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

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

于 2012-07-02T05:07:19.313 回答
28

将半透明主题分配给您希望在项目的 Android 清单文件中透明的活动:

<activity
    android:name="YOUR COMPLETE ACTIVITY NAME WITH PACKAGE"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />
于 2013-10-29T05:36:15.190 回答
18

就我而言,我必须根据某些条件在 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);
}

记住这里重要的一点:您必须setTheme()super.onCreate(savedInstanceState);. 我错过了这一点并坚持了 2 个小时,想着为什么我的主题没有在运行时反映出来。

于 2015-10-26T14:15:42.620 回答
16

我想补充一点,因为我也是一个新的 Android 开发人员。接受的答案很好,但我确实遇到了一些麻烦。我不确定如何将颜色添加到 colors.xml 文件中。这是应该如何完成的:

颜色.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 中查找标签“color”。我想我也应该提到这一点来帮助其他人。

于 2011-04-22T12:55:17.427 回答
16

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

不知道低版本...

于 2011-09-21T12:07:07.837 回答
11

2021 年事实

只需添加

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

你完成了。

windowIsFloating错了,这会产生一个 INSET 浮动窗口。

windowContentOverlay只与阴影有关。

windowIsTranslucent是错误的,它没有成功,所以你可以看到背后的活动。windowIsTranslucent 仅在动画转换时才相关。

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

colorBackgroundCacheHint无关紧要,除非在非常旧的设备上,无论如何默认值为 null。

于 2021-04-11T01:42:11.563 回答
9

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

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
于 2014-10-14T20:18:23.467 回答
8

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

<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
于 2012-02-19T14:12:04.230 回答
7

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

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

于 2013-06-12T10:54:14.197 回答
6

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

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

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

于 2017-04-25T17:14:00.683 回答
6

除了上述答案:

避免 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-09-26T09:55:01.073 回答
4

如果您正在使用,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"

有关更多详细信息,请阅读本文

于 2018-11-26T03:45:06.997 回答
3

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

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

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

    android:background="#80000000"
    

它非常适合我。

于 2013-09-02T07:06:47.593 回答
2

为其分配半透明主题

android:theme="@android:style/Theme.Translucent.NoTitleBar"
于 2013-07-25T06:31:14.877 回答
2

有两种方法:

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

使用Theme.NoDisplay仍然有效……但仅适用于较旧的 Android 设备。在 Android 6.0 及更高版本上,使用 Theme.NoDisplay 而不调用finish()会使onCreate() (or, technically, before onResume())您的应用程序崩溃。这就是为什么推荐使用Theme.Translucent.NoTitleBar不受此限制的原因。”</p>

于 2018-01-06T10:05:58.160 回答
1

注 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>
于 2015-12-11T09:32:27.747 回答
0

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

android:theme="@android:style/Theme.Translucent"
于 2016-08-16T14:21:46.560 回答
0

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

使用这个:

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

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

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

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

于 2019-07-30T11:16:17.137 回答
0

您可以setContentView(R.layout.mLayout)从活动中删除并将主题设置为android:theme="@style/AppTheme.Transparent". 检查此链接以获取更多详细信息。

于 2019-09-16T18:20:36.803 回答
0

只需将其放入 style.xml

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

或添加清单

<activity android:name=".usual.activity.Declaration" 
 android:theme="@android:style/Theme.Translucent.NoTitleBar" />
于 2021-07-09T15:33:09.783 回答
-1

除了gnobal 的上述解决方案外,我还必须在该特定活动的布局文件中将 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. 在 AndroidManifest.xml 中用上述样式设置 Activity 的主题

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

  3. 打开应用上述样式的 Activity 的布局文件,并将android:alpha="0"父布局元素的 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扩展您的活动才能使用上述解决方案。
于 2020-10-15T20:11:32.067 回答