且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在不使用AppCompact/ToolBar的情况下,ActionBar/StatusBar顶部的导航抽屉(如Android 5.0)

更新时间:2023-01-10 09:54:00

在常规Activity中,ActionBar是覆盖层View的一部分,该覆盖层是WindowDecorView.您可以从DecorView中删除此子项,将activity_main主布局填充到DecorView中,然后将覆盖View添加到DrawerLayoutFrameLayout中,从而有效地将抽屉放置在一切.

In a regular Activity, the ActionBar is part of an overlay View that is the only direct child of the Window's DecorView. You can remove this child from the DecorView, inflate the activity_main main layout into the DecorView, and then add the overlay View to the DrawerLayout's FrameLayout, effectively putting the drawer on top of everything.

为了避免对BaseLogeableActivity类进行更改,我们需要更改DrawerLayoutFrameLayout的ID,并确保存在container的资源ID以便动态分配给创建了用于保存FragmentFrameLayout.

In order to avoid making changes to the BaseLogeableActivity class, we'll need to change the ID of the DrawerLayout's FrameLayout, and ensure a Resource ID of container exists to assign to the dynamically created FrameLayout that will hold the Fragments.

根据需要创建container的资源ID:

Create the Resource ID of container, if necessary:

<item type="id" name="container" />

更改主布局的FrameLayout的ID:

<FrameLayout
    android:id="@+id/overlay_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

添加View杂耍代码的最简洁方法可能是仅覆盖MainActivitysetContentView()方法,如下所示:

The cleanest way to add the View juggling code is probably to just override MainActivity's setContentView() method, like so:

@Override
public void setContentView(int layoutResID) {
    ViewGroup decorView = (ViewGroup) getWindow().getDecorView();
    View overlayView = decorView.getChildAt(0);

    decorView.removeView(overlayView);
    getLayoutInflater().inflate(layoutResID, decorView, true);

    FrameLayout overlayContainer = (FrameLayout) findViewById(R.id.overlay_container);
    overlayContainer.addView(overlayView);

    FrameLayout container = new FrameLayout(this);
    container.setId(R.id.container);

    ViewGroup content = (ViewGroup) overlayView.findViewById(android.R.id.content);
    content.addView(container);
}

最后,如果您想让Activity覆盖状态栏,请在其主题中添加以下属性设置:

And finally, if you want your Activity to cover the Status Bar, add the following attribute setting to its theme:

<item name="android:windowFullscreen">true</item>

或者,由于您无法更改主题,因此请在setContentView()调用之前致电以下内容:

Or, since you can't change the theme, call the following before the setContentView() call:

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                     WindowManager.LayoutParams.FLAG_FULLSCREEN);