半透明活动填满整个屏幕

时间:2012-05-07 11:27:55

标签: android android-activity transparency android-theme

我希望有一个活动(2),半透明的方面超过另一个活动(1),在屏幕顶部对齐(4)。

enter image description here

我尝试将这些主题分配给活动编号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。

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

谁能告诉我怎样才能得到4?谢谢!

更新:这是我的活动2布局:

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

5 个答案:

答案 0 :(得分:22)

最后,这个主题可以得到像图像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>  

在我的活动2布局中,我可以设置android:background="@android:color/transparent"或者根本不设置任何值以使其正常工作。

感谢MikeIsrael和Veer的帮助。

答案 1 :(得分:10)

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

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

答案 2 :(得分: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="URTransparent" parent="Theme.AppCompat"> 
    // Copied from Theme.Translucent
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:colorBackgroundCacheHint">@null</item>
    <item name="android:windowIsTranslucent">true</item>

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

答案 3 :(得分:3)

首先拥有透明主题活动:

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

希望它有用。

答案 4 :(得分:0)

我不知道如何通过xml来实现,但这应该以编程方式工作,尝试将其添加到您的活动中(可能是活动的主视图)。

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

//remove the dim effect
lp.dimAmount = 0;
相关问题