我想在另一个活动之上创建一个透明的活动。
我怎样才能做到这一点呢?
我想在另一个活动之上创建一个透明的活动。
我怎样才能做到这一点呢?
当前回答
注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>
其他回答
所有这些答案可能会让人困惑,透明活动和无UI活动之间是有区别的。
用这个:
android:主题= " @android:风格/ Theme.Translucent.NoTitleBar”
将使活动透明,但将阻塞UI。
如果你想要一个None的UI活动,请使用这个:
android:主题= " @android:风格/主题。NoDisplay”
它是这样的:
<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
2021年的事实
只需添加
<item name="android:windowBackground">@android:color/transparent</item>
你就完成了。
windowIsFloating错误,这使得INSET浮动窗口。
windowcontenttoverlay只与阴影相关。
windowwistranslucent是错误的,它没有使它,所以你可以看到背后的活动。windowIsTranslucent只适用于动画转换。
backgroundDimEnabled使下面的活动变暗,但是,它在不同的设备上是完全错误的。(在某些情况下,除非你使用windowisfloat,否则它什么也不做;总的来说,这种行为是完全错误的/不确定的。)
colorBackgroundCacheHint是无关紧要的,除非在非常旧的设备上,默认是null。
有两种方法:
使用主题。NoDisplay 使用Theme.Translucent.NoTitleBar
使用主题。NoDisplay仍然可以工作,但只适用于旧的Android设备。Android 6.0及以上版本,使用Theme。没有调用onCreate()中的finish()(或者,从技术上讲,在onResume()之前)NoDisplay将使应用程序崩溃。这就是为什么建议使用theme .半透明。NoTitleBar,不受此限制。”
在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>