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

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


当前回答

我想补充一点,因为我也是一个新的Android开发人员。公认的答案很好,但我确实遇到了一些麻烦。我不确定如何在colors.xml文件中添加颜色。下面是应该怎么做:

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”。我想我也应该提到这一点,以帮助其他人。

其他回答

我只做了两件事,它让我的活动变得透明。它们在下面。

在清单文件中,我只是在活动标记中添加了以下代码。 android:主题= " @android:风格/ Theme.Translucent.NoTitleBar.Fullscreen” 然后我将该活动的主布局的背景设置为“#80000000”。就像 android:背景= " # 80000000 "

它非常适合我。

注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>

把它放在style.xml中

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

或在清单中添加

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

在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>

有两种方法:

使用主题。NoDisplay 使用Theme.Translucent.NoTitleBar

使用主题。NoDisplay仍然可以工作,但只适用于旧的Android设备。Android 6.0及以上版本,使用Theme。没有调用onCreate()中的finish()(或者,从技术上讲,在onResume()之前)NoDisplay将使应用程序崩溃。这就是为什么建议使用theme .半透明。NoTitleBar,不受此限制。”