我想在另一个Activity之上创建一个透明的Activity。
我该如何实现这一目标?
我想在另一个Activity之上创建一个透明的Activity。
我该如何实现这一目标?
只需让活动背景图片透明,或在XML文件中添加主题:
<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
除了上面的答案之外:
为了避免与Android Oreo相关的活动崩溃:
<style name="AppTheme.Transparent" parent="@style/Theme.AppCompat.Dialog">
<item name="windowNoTitle">true</item>
<item name="android:windowCloseOnTouchOutside">false</item>
</style>
<activity
android:name="xActivity"
android:theme="@style/AppTheme.Transparent" />
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
- Phill Wigginsandroid:theme="@android:style/Theme.Holo.Dialog"
。getWindow().setBackgroundDrawable(new ColorDrawable(0));
。对于对话框活动,我使用以下代码:
getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
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>
manifest
文件中,您可以像这样将主题添加到活动标签中。android:theme="@style/TransparentCompat"
for more details read this article
In the manifest file I just added the below code in the activity tag.
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
And I just set the background of the main layout for that activity as "#80000000". Like
android:background="#80000000"
它对我来说完美地工作。
android:theme="@android:style/Theme.Translucent.NoTitleBar"
使用Theme.NoDisplay
在旧版Android设备上仍然有效,但在Android 6.0及更高版本上,若在onCreate()
中(或者严格来说,在onResume()
之前)没有调用finish()
,则会导致应用崩溃。因此,推荐使用Theme.Translucent.NoTitleBar
,它不会受到这种限制的影响。
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke android:width="2dp" />
<gradient
android:angle="90"
android:endColor="#29000000"
android:startColor="#29000000" />
<corners
android:bottomLeftRadius="7dp"
android:bottomRightRadius="7dp"
android:topLeftRadius="7dp"
android:topRightRadius="7dp" />
</shape>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/test"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.09"
android:gravity="center"
android:background="@drawable/transperent_shape"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
android:theme="@android:style/Theme.Translucent"