我想在另一个活动之上创建一个透明的活动。

我怎样才能做到这一点呢?


当前回答

除了以上回答:

以避免安卓奥利奥相关的崩溃活动

<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" />

其他回答

其他解决方案对我有用,但我注意到一个问题,可能会影响一些人。当我像这样开始我的活动时:

Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);

而不是像这样的一行字

startActivity(new Intent(this, MainActivity.class));

我只得到黑色的背景,这很痛苦。我还没有弄清楚是什么导致了这个,我猜这是设备相关的(在小米上测试)。传递值也需要像这样。

startActivity(new Intent(this, MainActivity.class)
                            .putExtra("SOME_VALUE", value)
                            .putExtra("ANOTHER_VALUE", value2));

编辑: 将父样式更改为Theme.AppCompat.Light.NoActionBar似乎已经解决了这个问题

所有这些答案可能会让人困惑,透明活动和无UI活动之间是有区别的。

用这个:

android:主题= " @android:风格/ Theme.Translucent.NoTitleBar”

将使活动透明,但将阻塞UI。

如果你想要一个None的UI活动,请使用这个:

android:主题= " @android:风格/主题。NoDisplay”

把它放在style.xml中

<item name="android:windowBackground">@android:color/transparent</item>

或在清单中添加

<activity android:name=".usual.activity.Declaration" 
 android:theme="@android:style/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>

它是这样的:

<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />