我想在另一个活动之上创建一个透明的活动。
我怎样才能做到这一点呢?
我想在另一个活动之上创建一个透明的活动。
我怎样才能做到这一点呢?
当前回答
如果你正在使用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>
在清单文件中,您可以像这样将此主题添加到活动标签
android:theme="@style/TransparentCompat"
更多细节请阅读这篇文章
其他回答
把它放在style.xml中
<item name="android:windowBackground">@android:color/transparent</item>
或在清单中添加
<activity android:name=".usual.activity.Declaration"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
其他解决方案对我有用,但我注意到一个问题,可能会影响一些人。当我像这样开始我的活动时:
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
而不是像这样的一行字
startActivity(new Intent(this, MainActivity.class));
我只得到黑色的背景,这很痛苦。我还没有弄清楚是什么导致了这个,我猜这是设备相关的(在小米上测试)。传递值也需要像这样。
startActivity(new Intent(this, MainActivity.class)
.putExtra("SOME_VALUE", value)
.putExtra("ANOTHER_VALUE", value2));
编辑: 将父样式更改为Theme.AppCompat.Light.NoActionBar似乎已经解决了这个问题
随着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.在manifest中像这样声明你的活动:
<activity
android:name=".yourActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>
并在布局中添加透明背景。
对于对话活动,我使用这个:
getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
但是你也需要将活动中的主视图设置为不可见。否则,背景将是不可见的,而其中的所有视图将是可见的。