简体   繁体   English

Android构建带有图标的迷你导航抽屉

[英]Android build mini navigation drawer with Icons

I want to build a fixed mini nav drawer in android. 我想在android中建立一个固定的迷你导航抽屉。 will will always be visible an not extendable, with icons only. 将始终可见,且不可扩展,仅带有图标。

Something like https://github.com/mikepenz/MaterialDrawer Mini Drawer. https://github.com/mikepenz/MaterialDrawer迷你抽屉之类的东西。 I try used his library, but I can't figured out how to make it fixed. 我尝试使用他的库,但是我不知道如何修复它。

here is my code: 这是我的代码:

private Drawer result = null;
private MiniDrawer miniResult = null;


result = new DrawerBuilder()
                .withActivity(this)
                .withToolbar(toolbar)
                .withTranslucentStatusBar(false)
                .addDrawerItems(
                        new PrimaryDrawerItem().withName("1").withIcon(FontAwesome.Icon.faw_home).withIdentifier(1),
                        new PrimaryDrawerItem().withName("2").withIcon(FontAwesome.Icon.faw_home).withBadge("22").withBadgeStyle(new BadgeStyle(Color.RED, Color.RED)).withIdentifier(2).withSelectable(false),
                           new DividerDrawerItem(),
                        new ToggleDrawerItem().withName("3").withIcon(FontAwesome.Icon.faw_home).withChecked(true).withOnCheckedChangeListener(onCheckedChangeListener)
                ) // add the items we want to use with our Drawer
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                        if (drawerItem instanceof Nameable) {
                            Toast.makeText(MainActivity.this, ((Nameable) drawerItem).getName().getText(MainActivity.this), Toast.LENGTH_SHORT).show();
                        }
                        return false;
                    }
                })
                .withGenerateMiniDrawer(true)
                .withSavedInstance(savedInstanceState)
                // build only the view of the Drawer (don't inflate it automatically in our layout which is done with .build())
                .buildView();


        miniResult = result.getMiniDrawer();
        View view = miniResult.build(this);

As of the normal usage there is always the "normal" drawer (via the DrawerLayout ), and the MiniDrawer in your case you just want to use the MiniDrawer and add it to your View hierarchy. 从正常用法开始,总是有“普通”抽屉(通过DrawerLayout ),而对于您的情况, MiniDrawer只需要使用MiniDrawer并将其添加到View层次结构中。

As you already figured out correctly the MiniDrawer is filled via the normal DrawerBuilder as this comes with all the methods to interact with the elements added to the drawer. 正如您已经正确确定的那样, MiniDrawer是通过普通的DrawerBuilder填充的,因为它带有与添加到抽屉中的元素进行交互的所有方法。

As your use-case is special there is no "out-of-the-box" inflating of the MiniDrawer alone. 由于您的用例很特殊,因此MiniDrawer单独使用。

So you have the View of the MiniDrawer above. 因此,您具有上面的MiniDrawer View You now just need to add it to your Activity . 现在,您只需要将其添加到Activity

I recommend that your layout looks something like this: 我建议您的布局看起来像这样:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"/>
    <LinearLayout
        android:id="@+id/container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/toolbar">
        <RelativeLayout 
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <!-- Place your content here -->
        </RelativeLayout>
    </LinearLayout>
</RelativeLayout>

So in your code you get the "container" 因此,在您的代码中,您得到了“容器”

LinearLayout container = (LinearLayout) findViewById(R.id.container);
container.addView(view, 0); //view is the view of your MiniDrawer

Just to improve your code and remove unnecessary stuff. 只是为了改善您的代码并删除不必要的内容。 You can remove 您可以删除

.withToolbar(toolbar)
.withTranslucentStatusBar(false) 

as those are not necessary if you just use the MiniDrawer 因为如果您仅使用MiniDrawer

In the docs for that library the developer mentioned that you can lock the drawer . 在该库的文档中,开发人员提到您可以锁定抽屉 Could you try that out and see if it prevents it from opening? 您可以尝试一下,看看是否阻止它打开?

result = new DrawerBuilder()...
...
//get the DrawerLayout from the Drawer
DrawerLayout drawerLayout = result.getDrawerLayout();
//do whatever you want with the Drawer. Like locking it. 
drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);

Also try the other lock modes defined here if LOCK_MODE_CLOSED doesn't work and let us know what happens! 如果LOCK_MODE_CLOSED不起作用,请尝试使用此处定义的其他锁定模式,并告诉我们会发生什么!

You can create view for your drawer like this way,try to use ActionsContentView 您可以通过这种方式为抽屉创建视图,尝试使用ActionsContentView

<shared.ui.actionscontentview.ActionsContentView
        android:id="@+id/content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/divider"
        app:actions_layout="@layout/actions"
        app:actions_spacing="0dp"
        app:content_layout="@layout/content"
        app:shadow_drawable="@drawable/shadow"
        app:shadow_width="8dip"
        app:spacing="64dip"
        app:spacing_type="right_offset" />

OR try to use Partial SlidingPaneLayout 或尝试使用Partial SlidingPaneLayout

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM