【问题标题】:Android build mini navigation drawer with IconsAndroid 用图标构建迷你导航抽屉
【发布时间】:2016-07-13 03:55:07
【问题描述】:

我想在 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);

【问题讨论】:

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


    【解决方案1】:

    作为正常使用,总是有“正常”抽屉(通过DrawerLayout),而MiniDrawer在你的情况下你只想使用MiniDrawer并将它添加到你的View层次结构中.

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

    由于您的用例很特殊,因此不会单独对 MiniDrawer 进行“开箱即用”膨胀。

    所以你有上面MiniDrawerView。您现在只需将其添加到您的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) 
    

    因为如果您只使用MiniDrawer,这些都不是必需的

    【讨论】:

    • 迷你抽屉的宽度太大了,有办法手动定制吗?感谢您的评论,我真的很感激。
    • 所以你需要的少于72dp?所有MiniDrawer 都针对72dp 进行了优化和定义,可以更改它,但需要更多的工作
    • 听起来它现在正在工作。所以你可能会接受这个答案,并打开一个关于如何更改MiniDrawer 宽度的新问题,今天晚些时候也会为这个新问题添加一个详细的答案。也会帮助别人
    【解决方案2】:

    在该库的文档中,开发人员提到您可以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 的其他锁定模式,并告诉我们会发生什么!

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 2021-06-14
        • 1970-01-01
        • 1970-01-01
        • 2017-05-11
        • 1970-01-01
        • 2016-12-22
        • 2016-11-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多