如何启用或伪造 Material 设计指南 android.support.v4.widget.DrawerLayout 中的迷你变体?

How to enable or fake mini variant from Material Design guide for android.support.v4.widget.DrawerLayout?

如何从 Material Design guide 启用 "mini variant" - 以便在关闭状态下仅显示抽屉图标?

As a simple test project for my question I have taken the well-known Navigation Drawer Example by Google - 然后在右侧添加第二个 Drawer 并在两侧添加 ListView 条目的图标:

请告知如何激活(或者伪造?)"mini variant Drawer" - 这样在上面的屏幕截图的右侧只能看到音乐符号。

这是我的布局文件activity_main.xml:

<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout 
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v7.widget.Toolbar 
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

        <ImageView 
            android:src="@drawable/ic_music_note_black_24dp"
            android:onClick="openActions"
            android:layout_gravity="right"
            android:padding="16dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        </android.support.v7.widget.Toolbar>

        <FrameLayout
            android:id="@+id/content_frame"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:choiceMode="singleChoice" />

    <ListView
        android:id="@+id/right_drawer"
        android:layout_width="160dp"
        android:layout_height="match_parent"
        android:layout_gravity="end"
        android:choiceMode="singleChoice" />

</android.support.v4.widget.DrawerLayout>

并且 MainActivity.java 使用它:

public class MainActivity extends AppCompatActivity {
    private Toolbar mToolbar;
    private DrawerLayout mDrawerLayout;
    private ListView mDrawerList;
    private ListView mActionList;
    private ActionBarDrawerToggle mDrawerToggle;

    private String[] mPlanetTitles;
    private String[] mActions;
    private int[] mIcons;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mPlanetTitles = getResources().getStringArray(R.array.planets_array);
        mActions = getResources().getStringArray(R.array.music_actions);

        TypedArray ta = getResources().obtainTypedArray(R.array.music_icons);
        mIcons = new int[ta.length()];
        for (int i = 0; i < mIcons.length; i++)
            mIcons[i] = ta.getResourceId(i, R.drawable.ic_menu_black_24dp);
        ta.recycle();

        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(mToolbar);

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);
        mActionList = (ListView) findViewById(R.id.right_drawer);

        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
            R.layout.drawer_list_item, mPlanetTitles) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView view = (TextView) super.getView(position, convertView, parent);
                view.setCompoundDrawablePadding(24);
                view.setCompoundDrawablesWithIntrinsicBounds(
                    R.drawable.ic_stars_white_24dp, 0, 0, 0);
                return view;
            }
        });

        mActionList.setAdapter(new ArrayAdapter<String>(this,
            android.R.layout.simple_list_item_1, mActions) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                TextView view = (TextView) super.getView(position, convertView, parent);
                view.setCompoundDrawablePadding(24);
                view.setCompoundDrawablesWithIntrinsicBounds(mIcons[position], 0, 0, 0);
                return view;
            }
        });

        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  /* host Activity */
                mDrawerLayout,         /* DrawerLayout object */
                mToolbar,
                R.string.drawer_open,  /* "open drawer" description for accessibility */
                R.string.drawer_close  /* "close drawer" description for accessibility */
                ) {
            public void onDrawerClosed(View view) {
                mToolbar.setTitle(mTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                mToolbar.setTitle(mDrawerTitle);
                invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }
    }

这里是a video showing Gmail for tablets这个模式。

官方NavigationDrawer确实在他们的design specification中提到了'mini-variant',但是没有关于如何使用它的文档。也许稍后会作为支持库的一部分出现。如果/何时有官方解决方案,将更新答案。

到此为止,看看ActionsContentView library, does exactly what you want. Last time it was updates was 2 years ago, but it works, I have used it a while ago. You can also get it on Google Play并测试一下。

请访问以下link:

https://github.com/mikepenz/MaterialDrawer

https://github.com/mikepenz/MaterialDrawer/issues/487

MaterialDrawer 是创建 material 抽屉的实现。它的最新版本 4.0 虽然尚未发布,但提供了一个“嵌入式抽屉”来达到你想要的。到现在还不是很完美因为我已经下载了它的demo,试用了这个新功能,发现抽屉无法滑动打开,不过作者正在努力完成。

因此,您可以等待即将发布并查看其使用情况。

查看另一个 Mini nav variant 问题:Implementing Gmail Tablet like Navigation Drawer.

It appears to have a working solution for the Mini nav drawer variant just as it's found in the Gmail app for tablets as @Amol Gupta mentioned. The accepted answer in the other question contains a link to a blog post,其中包含有关如何实现 mini variant 的更详细说明。他们的解决方案使用在 "partial" 布局和 "full" 布局之间交叉淡入淡出的滑动窗格布局。

这里还有一个 link 博客 post 的示例来源:

https://github.com/chiuki/sliding-pane-layout

我建议将 @id/left_drawerlayout_width240dp 更改为更小的数字,例如 80dp