占满整个屏幕的半透明Activity

18

我想要在一个Activity(2)上方有一种半透明的效果,覆盖在另一个Activity(1)上面,并且处于屏幕的顶部位置(4)。

图片描述

我已经尝试为Activity 2分配这些主题:

<style name="Theme.CustomDialog" parent="android:style/Theme.Dialog">
    <item name="android:windowBackground">@android:color/black</item>
</style>  

<style name="CustomTheme">
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:background">@android:color/transparent</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:windowNoTitle">true</item>
</style>    

但结果总是3。

如果我在CustomTheme中设置<item name="android:windowIsFloating">false</item>,则结果为2。

有人能告诉我如何获得4吗?谢谢!

更新:这是我的第二个活动布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" android:background="#0000">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:background="#FFFFFF">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Menu" android:layout_centerHorizontal="true"/>

    </RelativeLayout>

</RelativeLayout>

https://dev59.com/35Xfa4cB1Zd3GeqPg48R - Mangesh
5个回答

28

最终,这个主题的效果就像图像4一样:

  <style name="Theme.CustomTranslucent" parent="android:style/Theme.Translucent">
        <item name="android:backgroundDimEnabled">true</item>
        <item name="android:backgroundDimAmount">0.5</item>
        <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>
        <item name="android:background">@android:color/transparent</item>
 </style>  
在我的第二个布局活动中,我可以设置android:background="@android:color/transparent",也可以不设置任何值来使其正常工作。
感谢MikeIsrael和Veer的帮助。

我刚刚按照你所写的做了,但是它不起作用。我有一个和你上一个答案中相似的样式。两个活动,但是由于某些缺失,它不是透明的 :( - Taochok

11

我已经阅读了其他的解决方案,但这是我的解决方案:

style.xml

<resources>
<style name="mytransparent.windowNoTitle" parent="android:Theme.Holo.Light">
    <item name="android:windowNoTitle">true</item>
    <item name="android:background">@android:color/transparent</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>
<style name="mytransparent.windowTitle" parent="android:Theme.Holo.Light">
    <item name="android:background">@android:color/transparent</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=".LoginActivity"
    android:theme="@style/mytransparent.windowTitle"
    android:configChanges="orientation"
    android:label="@string/title_activity_login"
    android:screenOrientation="portrait" ></activity>

5
如果你使用AppCompatActivity,那么你应该将父类设置为Theme.AppCompat。否则,应用程序可能会挂起或崩溃,并显示错误信息 (java.lang.RuntimeException: Unable to start activity ComponentInfo ... Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.)
在你的样式中添加以下代码:
<style name="MyTheme" parent="AppTheme.NoActionBar">
   <item name="android:windowIsTranslucent">true</item>
   <item name="android:windowBackground">@android:color/transparent</item>
   <item name="android:backgroundDimEnabled">false</item>
   <item name="android:windowNoTitle">true</item>
   <item name="android:windowActionBar">false</item>
   <item name="android:windowFullscreen">false</item>
   <item name="android:windowContentOverlay">@null</item>
</style>

然后将MyTheme添加到您的活动清单中: <activity android:name=".MyActivity" android:theme="@style/MyTheme">

4

首先需要有透明主题的活动:

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

在 Manifest 中为您的活动分配透明主题:
    <activity android:name=".MyActivity"
              android:label="@string/app_name"
              android:theme="@style/Theme.Transparent"/>

根据您的需求创建布局,并将活动的内容视图设置为该布局。

可以尝试以下布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@android:color/transparent">

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#FFFFFF"
        android:layout_alignParentTop="true">

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Menu" android:layout_centerHorizontal="true"/>

    </RelativeLayout>

</RelativeLayout>

希望您能受益。

它不起作用。我得到了我的菜单按钮在中间(像图像号码3),但没有半透明效果。 - Xavi Gil
你能发布一下包含菜单按钮的布局XML文件吗? - Veer
请尝试使用我回答中添加的布局。 - Veer
我仍然看到没有半透明效果的图像3。更准确地说,我看到红色背景,活动2位于屏幕中心。你尝试过你的建议吗?它对你有效吗? - Xavi Gil
我自己没有尝试过,但在我的一个项目中使用了透明主题。但是,它有对话框。您能否尝试在清单中使用Android定义的半透明主题android:theme="@android:style/Theme.Translucent"? - Veer

0

我不确定如何通过xml来实现,但是这应该可以通过编程来完成。尝试将此代码添加到您的活动中(可能是活动的主视图)。

//grab layout params
WindowManager.LayoutParams lp = this.getWindow().getAttributes();

//remove the dim effect
lp.dimAmount = 0;

我在我的活动中尝试了没有应用主题的情况,结果与图像2相同。使用我的CustomTheme应用后,我得到了第3个图像,没有半透明效果。 - Xavi Gil
@Xavi 嗯,也许问题不在活动中,而是主布局或其他什么地方。尝试在其中调用getWindow()怎么样?也许尝试给RelativeLayout添加一个id,然后通过findViewBy获取布局,再在布局上调用getWindow()。有任何变化吗? - MikeIsrael

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接