【发布时间】:2011-01-11 17:25:10
【问题描述】:
我想在另一个 Activity 之上创建一个透明的 Activity。
我怎样才能做到这一点?
【问题讨论】:
-
你能告诉我们透明活动有什么用
标签: android android-activity transparent
我想在另一个 Activity 之上创建一个透明的 Activity。
我怎样才能做到这一点?
【问题讨论】:
标签: android android-activity transparent
在您的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>
【讨论】:
<item name="android:windowBackground">@android:color/transparent</item>
android:windowIsFloating 设置为true。删除此属性以使其表现得像正常活动(在这种情况下,它将匹配 android:style/Theme.Translucent.NoTitleBar)
AppCompatActivity。所以parent="android:Theme" 让我的应用程序崩溃了。我刚刚删除了它,它就像魅力一样。谢谢!
是这样的:
<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)
在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>
在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:
<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 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 中查找标记“颜色”。我认为我也应该提及这一点以帮助其他人。
【讨论】:
我在 2.3.3 上通过在清单的活动标签中添加 android:theme="@android:style/Theme.Translucent" 来实现它。
我不知道低版本...
【讨论】:
AppCompatActivity。
只需添加
<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
【讨论】:
我只做了两件事,它使我的活动变得透明。它们在下面。
在清单文件中,我刚刚在 activity 标记中添加了以下代码。
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 而不在 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 中添加以下内容
<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>
用上面的样式设置activity的主题在 AndroidManifest.xml
<activity
android:name=".activityName"
android:theme="@style/Theme.Transparent"/>
打开应用了上述样式的 Activity 的布局文件,并将父布局元素的 alpha 值设置为 0 (android: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>
【讨论】: