Translucent Activity filling the entire screen

六月ゝ 毕业季﹏ 提交于 2019-11-30 05:11:43

Finally, this theme worked to get a result like image number 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>  

In my activity 2 layout, I could eihter set android:background="@android:color/transparent" or not set any value at all to make it work.

Thanks to MikeIsrael and Veer for their help.

arm

I've read the other solutions, but here is my solution:

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

If you use AppCompatActivity then you should use as parent Theme.AppCompat otherwise application can hang or crash with error (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.).

Put this in your styles:

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

Then add MyTheme to your activity manifest: <activity android:name=".MyActivity" android:theme="@style/MyTheme">

First have the Transparent Theme activity:

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

Assign the transparent theme to your activity in Manifest:

    <activity android:name=".MyActivity"
              android:label="@string/app_name"
              android:theme="@style/Theme.Transparent"/>

Create the layout as per your requirement and set the content view of your activity to that layout.

Try following layout:

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

Hope it is helpful.

I am not sure how to do it through the xml, but this should work programmatically, try adding this to your activity (maybe to the mainview of the activity).

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

//remove the dim effect
lp.dimAmount = 0;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!