如何在 Android 上创建透明的 Activity?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2176922/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do I create a transparent Activity on Android?
提问by UMAR
I want to create a transparent Activity on top of another activity.
我想在另一个活动之上创建一个透明的活动。
How can I achieve this?
我怎样才能做到这一点?
回答by gnobal
Add the following style in your res/values/styles.xml
file (if you don't have one, create it.) Here's a complete file:
在您的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>
(The value @color/transparent
is the color value #00000000
which I put in the res/values/color.xml
file. You can also use @android:color/transparent
in later Android versions.)
(该值@color/transparent
是#00000000
我放在res/values/color.xml
文件中的颜色值。您也可以@android:color/transparent
在以后的Android版本中使用。)
Then apply the style to your activity, for example:
然后将样式应用于您的活动,例如:
<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
...
</activity>
回答by yanchenko
It goes like this:
它是这样的:
<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
回答by Andrey
In the styles.xml:
在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>
In the AndroidManifest.xml:
在 AndroidManifest.xml 中:
<activity
android:name=".WhateverNameOfTheActivityIs"
android:theme="@style/Theme.AppCompat.Translucent">
...
</activity>
回答by Deepak Swami
Declare your activity in the manifest like this:
在清单中声明您的活动,如下所示:
<activity
android:name=".yourActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>
And add a transparent background to your layout.
并为您的布局添加透明背景。
回答by Jigar Pandya
Assign the translucent theme to the activity that you want to make transparent in the Android manifest file of your project:
将半透明主题分配给您要在项目的 Android 清单文件中使其透明的活动:
<activity
android:name="YOUR COMPLETE ACTIVITY NAME WITH PACKAGE"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
回答by Gem
In my case, i have to set the theme on the runtime in java based on some conditions. So I created one theme in style (similar to other answers):
就我而言,我必须根据某些条件在 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>
Then in Java I applied it to my activity:
然后在 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);
}
Remember one Important pointhere: You must call the setTheme()
function before super.onCreate(savedInstanceState);
. I missed this point and stucked for 2 hours, thinking why my theme is not reflected at run time.
请记住这里的一个重点:您必须setTheme()
在super.onCreate(savedInstanceState);
. 我错过了这一点并卡住了2个小时,想知道为什么我的主题在运行时没有反映出来。
回答by Camille Sévigny
I wanted to add to this a little bit as I am a new Android developer as well. The accepted answer is great, but I did run into some trouble. I wasn't sure how to add in the color to the colors.xml file. Here is how it should be done:
我想补充一点,因为我也是一名新的 Android 开发人员。接受的答案很好,但我确实遇到了一些麻烦。我不确定如何将颜色添加到 colors.xml 文件中。这是应该如何完成的:
colors.xml
颜色文件
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="class_zero_background">#7f040000</color>
<color name="transparent">#00000000</color>
</resources>
In my original colors.xml file I had the tag "drawable":
在我原来的 color.xml 文件中,我有标签“drawable”:
<drawable name="class_zero_background">#7f040000</drawable>
And so I did that for the color as well, but I didn't understand that the "@color/" reference meant look for the tag "color" in the XML. I thought that I should mention this as well to help anyone else out.
所以我对颜色也这样做了,但我不明白“@color/”引用意味着在 XML 中查找标签“颜色”。我想我也应该提到这一点以帮助其他人。
回答by androabhay
I achieved it on 2.3.3 by just adding android:theme="@android:style/Theme.Translucent"
in the activity tag in the manifest.
我在 2.3.3 上通过android:theme="@android:style/Theme.Translucent"
在清单中添加活动标签来实现它。
I don't know about lower versions...
我不知道低版本...
回答by Leebeedev
In the onCreatefunction, below the setContentView, add this line:
在onCreate函数中,在setContentView下方,添加以下行:
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
回答by jian
Just let the activity background image be transparent. Or add the theme in the XML file:
只需让活动背景图像透明即可。或者在 XML 文件中添加主题:
<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />