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

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


当前回答

随着gnobal的上述解决方案,我必须在该特定活动的布局文件中将alpha设置为0,因为在某些手机(红米Narzo 20 pro运行在Android 10上)屏幕的对话框部分显示的屏幕应该是透明的。出于某种原因,windowIsFloating导致了这个问题,但在删除它时,我没有得到所需的输出。

步骤:

Add the following in the style.xml located under res > values > styles.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> Set the theme of the activity with the above style in AndroidManifest.xml <activity android:name=".activityName" android:theme="@style/Theme.Transparent"/> Open your layout file of the activity on which you applied the above style and set it's alpha value to 0 (android:alpha="0") for the parent layout element. <?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>


请注意:

You'll have to extend you activity using

活动()

class and

不是AppCompatActivity

for using the above solution.

其他回答

它是这样的:

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

我想补充一点,因为我也是一个新的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”。我想我也应该提到这一点,以帮助其他人。

随着gnobal的上述解决方案,我必须在该特定活动的布局文件中将alpha设置为0,因为在某些手机(红米Narzo 20 pro运行在Android 10上)屏幕的对话框部分显示的屏幕应该是透明的。出于某种原因,windowIsFloating导致了这个问题,但在删除它时,我没有得到所需的输出。

步骤:

Add the following in the style.xml located under res > values > styles.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> Set the theme of the activity with the above style in AndroidManifest.xml <activity android:name=".activityName" android:theme="@style/Theme.Transparent"/> Open your layout file of the activity on which you applied the above style and set it's alpha value to 0 (android:alpha="0") for the parent layout element. <?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>


请注意:

You'll have to extend you activity using

活动()

class and

不是AppCompatActivity

for using the above solution.

注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:主题= " @android:风格/ Theme.Translucent.NoTitleBar.Fullscreen” 然后我将该活动的主布局的背景设置为“#80000000”。就像 android:背景= " # 80000000 "

它非常适合我。