如何在Android上创建透明活动?

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

我怎样才能做到这一点?


#1楼

在清单中声明您的活动,如下所示:

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

并为您的布局添加透明背景。


#2楼

我找到的最简单的方法是将AndroidManifest中的活动主题设置为android:theme="@android:style/Theme.Holo.Dialog"

然后在activity的onCreate方法中,调用getWindow().setBackgroundDrawable(new ColorDrawable(0));


#3楼

为它指定半透明主题

android:theme="@android:style/Theme.Translucent.NoTitleBar"

#4楼

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

  1. 在清单文件中,我刚刚在活动代码中添加了以下代码。

     android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen" 
  2. 我只是将该活动的主要布局背景设置为“ #80000000 ”。 喜欢

     android:background="#80000000" 

它非常适合我。


#5楼

将半透明主题分配给要在项目的Android清单文件中透明的活动:

<activity
    android:name="YOUR COMPLETE ACTIVITY NAME WITH PACKAGE"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

#6楼

它是这样的:

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

#7楼

onCreate函数中,在setContentView下面添加以下行:

getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

#8楼

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>

#9楼

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

#10楼

在我的情况下,我必须根据一些条件在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()函数super.onCreate(savedInstanceState); 。 我错过了这一点并且坚持了2个小时,想到为什么我的主题没有在运行时反映出来。


#11楼

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

#12楼

只需将以下行添加到清单文件中需要透明的活动标记即可。

android:theme="@android:style/Theme.Translucent"

#13楼

对于对话活动我使用:

getWindow().getDecorView().setBackgroundResource(android.R.color.transparent);

但是您还需要将活动中的主视图设置为不可见。 否则,背景将不可见,而其中的所有视图都将可见。


#14楼

有两种方式:

  1. 使用Theme.NoDisplay
  2. 使用Theme.Translucent.NoTitleBar

使用Theme.NoDisplay仍然可以工作......但仅适用于较旧的Android设备。 在Android 6.0及更高版本中,使用Theme.NoDisplay而不在onCreate() (or, technically, before onResume())调用finish() onCreate() (or, technically, before onResume())将使应用程序崩溃 。 这就是为什么建议使用Theme.Translucent.NoTitleBar ,它没有受到这种限制 。“


#15楼

除了以上答案:

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

#16楼

如果您正在使用AppCompatActivitystyles.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"

有关详细信息,请阅读本文


#17楼

所有这些答案可能会令人困惑,透明活动和无UI活动之间存在差异。

使用这个:

android:theme="@android:style/Theme.Translucent.NoTitleBar"

将使活动透明但会阻止UI。

如果您想要一个无UI活动而不是使用此:

android:theme="@android:style/Theme.NoDisplay"


#18楼

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


#19楼

我在2.3.3上通过在清单中的activity标签中添加android:theme="@android:style/Theme.Translucent"来实现它。

关于较低版本,我不知道......


#20楼

只需让活动背景图片透明即可。 或者在XML文件中添加主题:

<activity android:name=".usual.activity.Declaration" android:theme="@android:style/Theme.Translucent.NoTitleBar" />
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值