我想在另一个活动之上创建一个透明的活动。
我怎样才能做到这一点?
在您的文件中添加以下样式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>
(值是我放在文件中@color/transparent
的颜色值。你也可以在以后的Android版本中使用。)#00000000
res/values/color.xml
@android:color/transparent
然后将样式应用于您的活动,例如:
<activity android:name=".SampleActivity" android:theme="@style/Theme.Transparent">
...
</activity>
它是这样的:
<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
在样式.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>
在清单中声明您的活动,如下所示:
<activity
android:name=".yourActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"/>
并为您的布局添加透明背景。
将半透明主题分配给您希望在项目的 Android 清单文件中透明的活动:
<activity
android:name="YOUR COMPLETE ACTIVITY NAME WITH PACKAGE"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
就我而言,我必须根据某些条件在 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>
然后在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);
}
记住这里重要的一点:您必须setTheme()
在super.onCreate(savedInstanceState);
. 我错过了这一点并坚持了 2 个小时,想着为什么我的主题没有在运行时反映出来。
我想补充一点,因为我也是一个新的 Android 开发人员。接受的答案很好,但我确实遇到了一些麻烦。我不确定如何将颜色添加到 colors.xml 文件中。这是应该如何完成的:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="class_zero_background">#7f040000</color>
<color name="transparent">#00000000</color>
</resources>
在我原来的 colors.xml 文件中,我有标签“drawable”:
<drawable name="class_zero_background">#7f040000</drawable>
所以我对颜色也这样做了,但我不明白“@color/”引用意味着在 XML 中查找标签“color”。我想我也应该提到这一点来帮助其他人。
我在 2.3.3 上通过android:theme="@android:style/Theme.Translucent"
在清单中添加活动标签来实现它。
不知道低版本...
只需添加
<item name="android:windowBackground">@android:color/transparent</item>
你完成了。
windowIsFloating
错了,这会产生一个 INSET 浮动窗口。
windowContentOverlay
只与阴影有关。
windowIsTranslucent
是错误的,它没有成功,所以你可以看到背后的活动。windowIsTranslucent 仅在动画转换时才相关。
backgroundDimEnabled
使下面的活动变暗,但是,它在不同的设备上完全是错误的。(在某些情况下,除非您使用 windowIsFloating,否则它什么也不做;通常,该行为完全是错误的/不确定的。)
colorBackgroundCacheHint
无关紧要,除非在非常旧的设备上,无论如何默认值为 null。
在onCreate函数中,在setContentView下方,添加以下行:
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
只需让活动背景图像透明即可。或者在 XML 文件中添加主题:
<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
我发现最简单的方法是将 AndroidManifest 中的活动主题设置为android:theme="@android:style/Theme.Holo.Dialog"
.
然后在活动的 onCreate 方法中,调用getWindow().setBackgroundDrawable(new ColorDrawable(0));
.
对于对话活动,我使用这个:
getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);
但是您还需要将活动中的主视图设置为不可见。否则背景将不可见,而其中的所有视图都将可见。
除了上述答案:
避免 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" />
如果您正在使用,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"
有关更多详细信息,请阅读本文
我只做了两件事,它使我的活动变得透明。他们在下面。
在清单文件中,我刚刚在活动标签中添加了以下代码。
android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen"
我只是将该活动的主要布局的背景设置为“ #80000000 ”。像
android:background="#80000000"
它非常适合我。
为其分配半透明主题
android:theme="@android:style/Theme.Translucent.NoTitleBar"
使用Theme.NoDisplay
仍然有效……但仅适用于较旧的 Android 设备。在 Android 6.0 及更高版本上,使用 Theme.NoDisplay 而不调用finish()
会使onCreate() (or, technically, before onResume())
您的应用程序崩溃。这就是为什么推荐使用Theme.Translucent.NoTitleBar
不受此限制的原因。”</p>
注 1:在 Drawable 文件夹中创建 test.xml 并复制以下代码
<?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>
// 注意:角落和形状根据您的要求。
// 注2:创建xml:
<?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"
所有这些答案都可能令人困惑,透明活动和无 UI 活动之间存在差异。
使用这个:
android:theme="@android:style/Theme.Translucent.NoTitleBar"
将使活动透明,但会阻止 UI。
如果你想要一个无 UI 活动而不是使用这个:
android:theme="@android:style/Theme.NoDisplay"
您可以setContentView(R.layout.mLayout)
从活动中删除并将主题设置为android:theme="@style/AppTheme.Transparent"
. 检查此链接以获取更多详细信息。
只需将其放入 style.xml
<item name="android:windowBackground">@android:color/transparent</item>
或添加清单
<activity android:name=".usual.activity.Declaration"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
除了gnobal 的上述解决方案外,我还必须在该特定活动的布局文件中将 alpha 设置为 0,因为在某些手机(在 Android 10 上运行的 Redmi Narzo 20 pro)上,屏幕的对话框部分显示为应该是透明的。出于某种原因,windowIsFloating 导致了这个问题,但是在删除它时我没有得到想要的输出。
脚步:
在 res > values > styles.xml 下的 style.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>
在 AndroidManifest.xml 中用上述样式设置 Activity 的主题
<activity
android:name=".activityName"
android:theme="@style/Theme.Transparent"/>
打开应用上述样式的 Activity 的布局文件,并将android:alpha="0"
父布局元素的 alpha 值设置为 0 ( )。
<?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>