Translucent Activity filling the entire screen

╄→尐↘猪︶ㄣ 提交于 2019-11-29 02:32:47

问题


I'd like to have an activity (2) with translucent aspect over another activity (1), aligned at the top of the screen (4).

I have tried assigning these themes to activity number 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>    

But the result is always 3.

If I set <item name="android:windowIsFloating">false</item> in the CustomTheme the result is 2.

Can anybody tell me how can I get 4? Thanks!

UPDATE: This is my activity 2 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: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>

回答1:


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.




回答2:


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>



回答3:


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




回答4:


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.




回答5:


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;


来源:https://stackoverflow.com/questions/10481277/translucent-activity-filling-the-entire-screen

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