获取DecorView方法在Lollipop版本中是否包含导航栏视图?

11

我使用SlidingMenu来实现我的侧滑菜单。

代码如下:

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);
}
然后我遇到了一个问题,我的布局在导航栏后面。 我的底部视图在导航栏后面我的底部布局在导航栏后面

我将SlidingMenu.SLIDING_WINDOW更改为SlidingMenu.SLIDING_CONTENT。 这有效,但是操作栏始终在顶部。

查看SlidingMenu的源代码,我找到了添加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;
    }

我该如何修复它? 这个 bug 只出现在 Android 5.0 棒棒糖版本。

5个回答

22

您可以通过以下方式避免此问题:

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

完美地工作了...非常好的答案。 - Santhosh
如果将android:windowDrawsSystemBarBackgrounds设置为false,则状态栏的颜色会消失。 - Zeeshan

4

GitHub上的SlidingMenu开放了相同的问题

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);
    } 
} 

3

最好的决定是在上面链接的问题中进行评论。不要使用那里的修复! - Den Drobiazko

1
结论:没有解决方案。只有hackish的解决方法,你将得到以下效果之一: - 状态栏未着色 - 在没有软导航栏的设备上会出现额外的不必要的填充 - 完全隐藏导航布局
以上都不是解决方案。丑陋、无用的hack。真的没有解决方案吗?真的吗?我可以看到许多应用程序,包括Android系统拨号或短信应用程序 - 它们能够显示彩色状态栏,导航界面从不隐藏,在没有软导航栏的设备上也没有丑陋的填充。使用Cardview、ListView,一切正常。
到底怎么可能!

1
这个很好用,只需计算Lollipop设备的导航栏高度并将其添加到paddingBottom中即可。 Android ResideMenu库,在Fragment底部存在裁剪问题
@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;
    }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接