Android构建带有图标的迷你导航抽屉 [英] Android build mini navigation drawer with Icons

查看:182
本文介绍了Android构建带有图标的迷你导航抽屉的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在android中建立一个固定的迷你导航抽屉.将始终可见,且不可扩展,仅带有图标.

I want to build a fixed mini nav drawer in android. will will always be visible an not extendable, with icons only.

类似于 https://github.com/mikepenz/MaterialDrawer 迷你抽屉. 我尝试使用他的库,但是我不知道如何修复它.

Something like https://github.com/mikepenz/MaterialDrawer Mini Drawer. I try used his library, but I can't figured out how to make it fixed.

这是我的代码:

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

推荐答案

从正常使用开始,总是有正常"抽屉(通过DrawerLayout),而MiniDrawer在您的情况下,您只是想使用MiniDrawer并将其添加到您的View层次结构中.

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.

正如您已经正确计算出的那样,MiniDrawer是通过普通的DrawerBuilder填充的,因为这是与添加到抽屉中的元素进行交互的所有方法所附带的.

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夸大其词.

As your use-case is special there is no "out-of-the-box" inflating of the MiniDrawer alone.

因此,您具有上面MiniDrawerView.现在,您只需将其添加到您的Activity.

So you have the View of the MiniDrawer above. You now just need to add it to your 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) 

,因为如果您只使用MiniDrawer

这篇关于Android构建带有图标的迷你导航抽屉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆