The getDecorView method return view include naviga

2019-06-16 03:30发布

问题:

I use SlidingMenu to implement my slide-in menus.

The code is

private void initSlidingMenu()
{
    // configure the SlidingMenu
    menu = new SlidingMenu(this);
    menu.setMode(SlidingMenu.LEFT);
    menu.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
    menu.setShadowWidthRes(R.dimen.shadow_width);
    //        menu.setShadowDrawable(R.drawable.shadoew);
    menu.setBehindOffsetRes(R.dimen.slidingmenu_offset);
    //        menu.setFadeDegree(0.35f);
    menu.attachToActivity(this, SlidingMenu.SLIDING_WINDOW);
    menu.setMenu(R.layout.menu_main_sliding);
}

Then I got a problem is my layout behind of navigation bar.

And i change the SlidingMenu.SLIDING_WINDOW to SlidingMenu.SLIDING_CONTENT. It's works,but the actionbar always on the top.

Look at the source code of SlidingMenu,i find this code to add slidingmenu.

    switch (slideStyle) {
    case SLIDING_WINDOW:
        mActionbarOverlay = false;
        ViewGroup decor = (ViewGroup) activity.getWindow().getDecorView();
        ViewGroup decorChild = (ViewGroup) decor.getChildAt(0);
        // save ActionBar themes that have transparent assets
        decorChild.setBackgroundResource(background);
        decor.removeView(decorChild);
        decor.addView(this);
        setContent(decorChild);
        break;
    case SLIDING_CONTENT:
        mActionbarOverlay = actionbarOverlay;
        // take the above view out of
        ViewGroup contentParent = (ViewGroup)activity.findViewById(android.R.id.content);
        View content = contentParent.getChildAt(0);
        contentParent.removeView(content);
        contentParent.addView(this);
        setContent(content);
        // save people from having transparent backgrounds
        if (content.getBackground() == null)
            content.setBackgroundResource(background);
        break;
    }

How can i fix it? This bug only found in Android 5.0 lollipop.

回答1:

SlidingMenu on GitHub has opened same issue.

private int getNavigationBarHeight() { 
    Resources resources = getResources();
    int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
    if (resourceId > 0) {
        return resources.getDimensionPixelSize(resourceId);
    } 
    return 0; 
} 

@Override 
public void onCreate(Bundle savedInstanceState) {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
        int navBarHeight = getNavigationBarHeight();
        findViewById(R.id.base_frame).setPadding(0, 0, 0, navBarHeight);
        findViewById(R.id.menu_frame).setPadding(0, 0, 0, navBarHeight);
    } 
} 


回答2:

You could avoid this styling your activity like this:

<!-- values-v21/styles.xml -->
<resources xmlns:android="http://schemas.android.com/apk/res/android">

    <style name="Theme" parent="FrameworkRoot.Theme">
        <item name="android:windowDrawsSystemBarBackgrounds">false</item>
    </style>

</resources>


<!-- AndroidManifest.xml -->
<activity
        android:name="com.yourpackage.YourActivity"
        android:theme="Theme"
        android:screenOrientation="portrait" />


回答3:

There is a good workaround for both Hardware and Software Navigation Bar.

if(Build.VERSION.SDK_INT >= 21)
    setSystemUiVisibility(SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION);

See: https://github.com/jfeinstein10/SlidingMenu/issues/680#issuecomment-73912402



回答4:

this one works fine, just calculate navigation bar height for Lollipop devices and add this to paddingBottom

Android ResideMenu library, bottom of Fragment has Cropping issue

@Override
    protected boolean fitSystemWindows(Rect insets) {
        int bottomPadding=insets.bottom;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Resources resources = getResources();
            int resourceId = resources.getIdentifier("navigation_bar_height", "dimen", "android");
            if (resourceId > 0) {
                bottomPadding += resources.getDimensionPixelSize(resourceId);
            }
        }
        this.setPadding(viewActivity.getPaddingLeft() + insets.left, viewActivity.getPaddingTop() + insets.top,
                viewActivity.getPaddingRight() + insets.right, viewActivity.getPaddingBottom() + bottomPadding);
        insets.left = insets.top = insets.right = insets.bottom = 0;
        return true;
    }


回答5:

Conclusion: there is no solution. Only hackish workarounds, you will get one of below effects: - status bar not colored - extra unnecesary padding on devices without soft navbar - completly hidden navigation layout

None of above is a solution. Ugly, useless hacks. Really, no solution? Really? I can see many apps, including Android system dialer or sms app - they are able to display colored status bar, navigation ui never hides and no ugly padding on devices without soft navbar. Cardview, ListView used, all ok.

How the hell is it possible!