How to create a transparent activity WITHOUT windowIsFloating

后端 未结 3 751
再見小時候
再見小時候 2021-01-31 10:47

windowIsFloating while a great one stop shop for creating Dialog styled UI\'s has many ---bugs--- quirks.

The one which I\'m battling right no

相关标签:
3条回答
  • 2021-01-31 11:16

    Thanks to @PolamReddy who nudged me towards answer I wanted:

    The theme Theme.Translucent.NoTitleBar.Fullscreen and its ancestors contains all the attributes you need to create a translucent window. In order to get everything apart from windowIsFloating I went through the ancestor stack and pulled out the entire set of attributes:

    <style name="Theme.CustomTheme.TransparentActivity">
        <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>
        <item name="android:windowNoTitle">true</item>
        <item name="android:windowContentOverlay">@null</item>
        <item name="android:windowFullscreen">true</item>
    </style> 
    

    This style must be assigned to the Activity in the AndroidManifest.xml rather than the root view of a layout.

    0 讨论(0)
  • 2021-01-31 11:20

    use like this for activity in the manifest file ( theme represents the transparent of that activity.)

    <activity android:name=".Settings"      
              android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
    
    0 讨论(0)
  • 2021-01-31 11:35

    The accepted answer works fine, until I need to have an EditText inside the dialog-style window, as the soft IME keyboard would not push the "dialog" up without windowIsFloating. So I have to tackle the 'bug' head-on.

    As with the case in the question, I need to have the width set to "match_parent" while the height can remain "wrap_content". Using windowIsFloating, I end up setting up a ViewTreeObserver, traverse up the view tree during layout, and force the floating window to the desired width. The approach is as follows (under onCreate() of the Activity) -

    setContentView(contentView);
    
    // set up the ViewTreeObserver
    ViewTreeObserver vto = contentView.getViewTreeObserver();
    vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        int displayWidth = -1;
        DisplayMetrics metrics = null;
        View containerView = null;
    
        @Override
        public void onGlobalLayout() {            
            if (containerView == null) {
                // traverse up the view tree
                ViewParent v = contentView.getParent();
                containerView = contentView;
                while ((v != null) && (v instanceof View)) {
                    containerView = (View) v;
                    v = v.getParent();
                }
            }
            if (metrics == null) {
                metrics = new DisplayMetrics();
            }
            Display display = getWindowManager().getDefaultDisplay();
            display.getMetrics(metrics);
            if (displayWidth != metrics.widthPixels) {
                displayWidth = metrics.widthPixels;
                WindowManager.LayoutParams params = 
                        (WindowManager.LayoutParams) containerView.getLayoutParams();
                // set the width to the available width to emulate match_parent
                params.width = displayWidth;
                // windowIsFloating may also dim the background
                // do this if dimming is not desired
                params.dimAmount = 0f;
                containerView.setLayoutParams(params);
                getWindowManager().updateViewLayout(containerView, params);
            }
        }
    });
    

    This works up to Android 7 so far (even in split screen mode), but one may catch the possible cast exception for casting to WindowManager.LayoutParams in case future implementation changes, and add removeOnGlobalLayoutListener(); in an appropriate place.

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