如何在Android上创建一个透明的Activity?

1016

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

我该如何实现这一目标?


你能告诉我们透明活动的用途是什么吗? - Mohamed Ben Romdhane
25个回答

8

只需让活动背景图片透明,或在XML文件中添加主题:

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

7

除了上面的答案之外:

为了避免与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" />

2
截至2018年,这应该是最佳答案 - Geek Guy
1
在 API 28 的模拟器上给我一个黑色背景。 - Someone Somewhere
我尝试了这个方法来解决在Android 8.0中设置方向导致崩溃的问题,但我仍然收到IllegalStateException:只有全屏不透明活动请求方向的错误。 - Rahul Sahni
非常好用。在安卓11的三星S20上测试过了。虚拟机上仍然显示黑屏,但其他情况下只是略微变暗的屏幕。 - Phill Wiggins
1
添加下面的代码应该可以去除对话框主题的遮罩效果。 window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND) - Phill Wiggins

7
我发现最简单的方法是在AndroidManifest中将活动的主题设置为android:theme="@android:style/Theme.Holo.Dialog"
然后在活动的onCreate方法中,调用getWindow().setBackgroundDrawable(new ColorDrawable(0));

6

对于对话框活动,我使用以下代码:

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

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

7
将背景完全变成黑色。 - ucMedia

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

for more details read this article


当我使用以上代码时,透明度效果非常好,但它并没有显示上一个画板的透明度,而是显示了我的主屏幕(壁纸屏幕)的背景。 - Karthickyuvan
这是我的代码 https://stackoverflow.com/questions/63326191/android-how-to-make-a-custom-layout-as-transparency - Karthickyuvan

3
我刚刚完成了两件事情,使得我的活动变得透明。它们如下所示。
  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"
    

它对我来说完美地工作。


2
将其分配给半透明主题。
android:theme="@android:style/Theme.Translucent.NoTitleBar"

2

有两种方法:

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

使用Theme.NoDisplay在旧版Android设备上仍然有效,但在Android 6.0及更高版本上,若在onCreate()中(或者严格来说,在onResume()之前)没有调用finish(),则会导致应用崩溃。因此,推荐使用Theme.Translucent.NoTitleBar,它不会受到这种限制的影响。


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>

0
只需在需要透明的清单文件中的活动标签中添加以下行即可。
android:theme="@android:style/Theme.Translucent"

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接