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

时间:2016-07-13 03:55:07

标签: java android navigation-drawer android-custom-view materialdrawer

我想在android中构建一个固定的迷你导航抽屉。将始终显示不可扩展,仅带图标。

类似https://github.com/mikepenz/MaterialDrawer迷你抽屉的东西。 我尝试使用他的库,但我无法弄清楚如何修复它。

这是我的代码:

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

3 个答案:

答案 0 :(得分:2)

在正常使用情况下,始终存在"正常"抽屉(通过DrawerLayout)和MiniDrawer您只想使用MiniDrawer并将其添加到View层次结构中。

正如您已经正确理解的那样,MiniDrawer通过普通DrawerBuilder填充,因为这与所有与添加到抽屉的元素进行交互的方法一起提供。

由于您的用例很特殊,所以没有开箱即用的#34;单独膨胀MiniDrawer

所以你拥有上面View的{​​{1}}。您现在只需将其添加到MiniDrawer

我建议您的布局如下所示:

Activity

因此,在您的代码中,您将获得"容器"

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

只是为了改进您的代码并删除不必要的东西。你可以删除

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

如果您只使用.withToolbar(toolbar) .withTranslucentStatusBar(false)

,则不需要这些

答案 1 :(得分:0)

在该图书馆的文档中,开发人员提到您可以lock the drawer。你可以尝试一下,看看是否会阻止它打开吗?

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

如果LOCK_MODE_CLOSED不起作用,请尝试定义here的其他锁定模式,让我们知道会发生什么!

答案 2 :(得分:0)

您可以像这样为抽屉创建视图,尝试使用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" />

或尝试使用Partial SlidingPaneLayout