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

时间:2015-09-28 08:09:12

标签: android translucency android-statusbar materialdrawer

我正在使用MaterialDrawer库为我的活动添加抽屉。活动必须具有半透明状态栏。如下图所示:

enter image description here

这是我活动的最重要部分,当时尚未添加库。

当我使用库添加抽屉时:

X509Store rootStore = new X509Store(StoreName.Root, StoreLocation.LocalMachine);
rootStore.Open(OpenFlags.ReadWrite);
rootStore.Add(certificate);
rootStore.Close();

public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); DrawerMenu.addTo(this); } } 助手类:

DrawerMenu

我会得到这个结果:

enter image description here

状态栏显然不是半透明的,活动内容不低于它。

然而,当我打开抽屉时,它会进入状态栏下方:

enter image description here

这也是我申请活动的主题:

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

这里有什么问题?

1 个答案:

答案 0 :(得分:1)

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

因此,使用您发布的屏幕截图,您可以使用 MaterialDrawer 的基本配置。 在大多数情况下,使用抽屉的人希望他们的内容(包括工具栏)显示在 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 是半透明的。然后你必须做以下。 使用其中一个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