更新时间:2023-01-10 09:54:00
在常规Activity
中,ActionBar
是覆盖层View
的一部分,该覆盖层是Window
的DecorView
.您可以从DecorView
中删除此子项,将activity_main
主布局填充到DecorView
中,然后将覆盖View
添加到DrawerLayout
的FrameLayout
中,从而有效地将抽屉放置在一切.
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
类进行更改,我们需要更改DrawerLayout
的FrameLayout
的ID,并确保存在container
的资源ID以便动态分配给创建了用于保存Fragment
的FrameLayout
.
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 Fragment
s.
根据需要创建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
杂耍代码的最简洁方法可能是仅覆盖MainActivity
的setContentView()
方法,如下所示:
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);