我想在另一个活动之上创建一个透明的活动。
我怎样才能做到这一点?
在您的 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
是我放在 res/values/color.xml
文件中的颜色值 #00000000
。您也可以在以后的 Android 版本中使用 @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" />
50% black should be #7f000000
。每个组件(A、R、G、B)都可以从 0-255
中获取值。 50% of 255 = 127. 127 in Hex = 7F
那如何计算透明度(opacity)
在样式.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>
Theme.Appcompat.Light.NoActionBar
继承。
background
并将您喜欢的半透明颜色放入 windowBackground
在清单中声明您的活动,如下所示:
<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);
}
请记住这里重要一点:您必须在 super.onCreate(savedInstanceState);
之前调用 setTheme()
函数。我错过了这一点并坚持了 2 个小时,想着为什么我的主题没有在运行时反映出来。
我想补充一点,因为我也是一个新的 Android 开发人员。接受的答案很好,但我确实遇到了一些麻烦。我不确定如何将颜色添加到 colors.xml 文件中。这是应该如何完成的:
颜色.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"
来实现它。
不知道低版本...
AppCompatActivity
。
2021 年事实
只需添加
<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"
。
然后在 Activity 的 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" />
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND)
如果您使用的是 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"
有关详细信息,请阅读此article
我只做了两件事,它使我的活动变得透明。他们在下面。
在清单文件中,我刚刚在活动标签中添加了以下代码。 android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" 我只是将该活动的主布局的背景设置为“#80000000”。像 android:background="#80000000"
它非常适合我。
为其分配半透明主题
android:theme="@android:style/Theme.Translucent.NoTitleBar"
有两种方法:
使用 Theme.NoDisplay 使用 Theme.Translucent.NoTitleBar
使用 Theme.NoDisplay
仍然有效……但仅限于较旧的 Android 设备。在 Android 6.0 及更高版本上,使用 Theme.NoDisplay 而不在 onCreate() (or, technically, before onResume())
中调用 finish()
将会崩溃您的应用。这就是建议使用不受此限制影响的Theme.Translucent.NoTitleBar
的原因。”
注 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"
。查看此 link 了解更多详情。
只需将其放入 style.xml
<item name="android:windowBackground">@android:color/transparent</item>
或添加清单
<activity android:name=".usual.activity.Declaration"
android:theme="@android:style/Theme.Translucent.NoTitleBar" />
除了上述 gnobal's 解决方案外,我还必须在该特定活动的布局文件中将 alpha 设置为 0,因为在某些手机(在 Android 10 上运行的 Redmi Narzo 20 pro)上的对话框部分屏幕显示应该是透明的屏幕。出于某种原因,windowIsFloating 导致了这个问题,但是在删除它时我没有得到想要的输出。
脚步:
在 res > values > styles.xml 下的 style.xml 添加以下内容 在 AndroidManifest.xml 中使用上述样式设置活动的主题
请注意:
活动()
不是 AppCompatActivity
<item name="android:windowBackground">@android:color/transparent</item>
android:windowIsFloating
设置为 true。删除此属性以使其行为类似于正常活动(在这种情况下,它将匹配android:style/Theme.Translucent.NoTitleBar
)AppCompatActivity
。所以parent="android:Theme"
使我的应用程序崩溃。我刚刚删除了它,它就像魅力一样。谢谢!