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

我怎样才能做到这一点呢?


当前回答

只要让活动背景图像是透明的。或者在XML文件中添加主题:

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

其他回答

你可以从你的活动中删除setContentView(r.b ayout. mlayout),并设置主题为android:theme="@style/AppTheme.Transparent"。查看这个链接了解更多细节。

只需将下面一行添加到清单文件中的activity标记中,它需要看起来透明。

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

在我的情况下,我必须根据一些条件在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个小时,思考为什么我的主题没有在运行时反映出来。

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

步骤:

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> Set the theme of the activity with the above style in AndroidManifest.xml <activity android:name=".activityName" android:theme="@style/Theme.Transparent"/> 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>


请注意:

You'll have to extend you activity using

活动()

class and

不是AppCompatActivity

for using the above solution.

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