MaterialDrawer 库将半透明状态栏变成不透明状态栏

MaterialDrawer library turns a translucent status bar into a opaque one

我正在使用 MaterialDrawer 库将抽屉添加到我的活动中。活动必须有半透明的状态栏。喜欢下图:

这是我的 activity 的顶部部分,当时库还没有添加到它。

当我使用库添加抽屉时:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DrawerMenu.addTo(this);
    }

}

还有 DrawerMenu 助手 class:

public class DrawerMenu {
    public static void addTo(final Activity activity) {
        AccountHeader headerResult = new AccountHeaderBuilder()
            .withActivity(activity)
            .withHeaderBackground(R.drawable.drawer_header)
            .addProfiles(
                    new ProfileDrawerItem()
                            .withName("Ashkan")
                            .withEmail("ashkan@sarlak.com")
                            .withIcon(ContextCompat.getDrawable(activity, R.drawable.profile_pic))
            )
            .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                @Override
                public boolean onProfileChanged(View view, IProfile profile, boolean currentProfile) {
                    return false;
                }
            })
            .build();

        new DrawerBuilder()
            .withActivity(activity)
            .withAccountHeader(headerResult)
            .addDrawerItems(new PrimaryDrawerItem().withName("Login"))
            .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                @Override
                public boolean onItemClick(View view, int i, IDrawerItem iDrawerItem) {
                    Toast.makeText(activity, "Login!!!", Toast.LENGTH_LONG).show();
                    return true;
                }
            })
            .build();
    }
}

我会得到这个结果:

状态栏明显不透明,activity下面没有内容

但是当我打开抽屉时,它会在状态栏下面:

这也是我应用于 activity:

的主题
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
    <item name="android:windowTranslucentStatus">true</item>
    <item name="android:windowTranslucentNavigation">false</item>
</style>

这里有什么问题?

您发布的屏幕截图看起来您的应用应该以全屏模式显示。

因此,对于您发布的屏幕截图,您使用了 MaterialDrawer 的基本配置。 在大多数情况下,使用抽屉的人希望他们的内容(包括 Toolbar)显示在 StatusBar 下面,但 drawer StatusBar 后面。所以这里基本上发生的是 MaterialDrawer 的逻辑会将您的应用程序设置为半透明 StatusBar 模式,因此内容将移动到它后面。此外,它将添加 ScrimInsetsFrameLayout,然后将填充添加到顶部,因此内容将低于 StatusBar

在您的情况下,您希望您的内容也位于 StatusBar 之后,因此您也希望将您的内容移至 StatusBar 之后。

这可以通过将 withFullscreen(true) 标志添加到 MaterialDrawer

Builder 来实现

这是程序化的方式

//Create the drawer
result = new DrawerBuilder()
    .withActivity(this)
    .withFullscreen(true)
    .withSavedInstance(savedInstanceState)
    .build();

这也会使您的 NavigationBar 半透明。

关于这个主题有一个未解决的问题,它可能也会改进这里的行为。 https://github.com/mikepenz/MaterialDrawer/issues/698

因此实施一个附加标志以不包括 ScrimInsetsFrameLayout https://github.com/mikepenz/MaterialDrawer/issues/698#issuecomment-143674729

好的,作为补充。如果您只希望 StatusBar 是半透明的。那么你必须做以下事情。 为此 activity

使用 TranslucentStatus 主题之一(或其子主题)
@style/MaterialDrawerTheme.Light.TranslucentStatus

然后构建抽屉,如下所示:

//Create the drawer
result = new DrawerBuilder()
    .withActivity(this)
    .withFullscreen(true)
    .withTranslucentNavigationBarProgrammatically(false)
//note that the translucentNavigationBarProgrammatically comes AFTER the withFullscreen method

这将导致透明的 StatusBar 但带有黑色 NavigationBar