我想在另一个活动之上创建一个透明的活动。
我怎样才能做到这一点呢?
我想在另一个活动之上创建一个透明的活动。
我怎样才能做到这一点呢?
当前回答
只需将下面一行添加到清单文件中的activity标记中,它需要看起来透明。
android:theme="@android:style/Theme.Translucent"
其他回答
在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>
其他解决方案对我有用,但我注意到一个问题,可能会影响一些人。当我像这样开始我的活动时:
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似乎已经解决了这个问题
我发现的最简单的方法是在AndroidManifest中设置活动的主题为android:theme="@android:style/ theme . holo . dialog "。
然后在活动的onCreate方法中,调用getWindow()。setBackgroundDrawable(新ColorDrawable(0));。
对于对话活动,我使用这个:
getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
但是你也需要将活动中的主视图设置为不可见。否则,背景将是不可见的,而其中的所有视图将是可见的。
只需将下面一行添加到清单文件中的activity标记中,它需要看起来透明。
android:theme="@android:style/Theme.Translucent"