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

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


当前回答

在onCreate函数中,在setContentView的下面,添加这一行:

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

其他回答

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

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

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

我发现的最简单的方法是在AndroidManifest中设置活动的主题为android:theme="@android:style/ theme . holo . dialog "。

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

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

在manifest中像这样声明你的活动:

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

并在布局中添加透明背景。

在onCreate函数中,在setContentView的下面,添加这一行:

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));