Translucent Activity filling the entire screen

后端 未结 5 1131
忘掉有多难
忘掉有多难 2020-12-29 08:13

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

相关标签:
5条回答
  • 2020-12-29 08:58

    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.

    0 讨论(0)
  • 2020-12-29 08:59

    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>
    
    0 讨论(0)
  • 2020-12-29 09:01

    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.

    0 讨论(0)
  • 2020-12-29 09:06

    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;
    
    0 讨论(0)
  • 2020-12-29 09:07

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

    0 讨论(0)
提交回复
热议问题