适用于 KitKat 的 Android 5.0 Material Design 风格导航抽屉

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26476837/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 11:34:06  来源:igfitidea点击:

Android 5.0 material design style navigation drawer for KitKat

androidnavigation-drawerandroid-5.0-lollipop

提问by nomongo

I see that Android introduced new navigation drawer icons, drawer icon and back arrow icon. How can we use that in Kitkat supported apps. See Google's latest version of Newsstand app, which has the latest navigation drawer icons and animations. How can we implement that?

我看到 Android 引入了新的导航抽屉图标、抽屉图标和后退箭头图标。我们如何在 Kitkat 支持的应用程序中使用它。查看 Google 最新版 Newsstand 应用程序,其中包含最新的导航抽屉图标和动画。我们如何实施?

I have tried setting the minSDK to 19 and complileSDK to 21 but it's using the old style icons. Is that self implemented?

我曾尝试将 minSDK 设置为 19 并将 complileSDK 设置为 21,但它使用的是旧样式图标。那是自己实现的吗?

回答by jpardogo

You need to use the new Toolbar in the appcompat v21 and the new ActionBarDrawerTogglethat is in this library as well.

您需要使用 appcompat v21 中的新工具栏ActionBarDrawerToggle以及此库中的新工具栏。

Add the gradle dependency to your gradle file:

将 gradle 依赖项添加到您的 gradle 文件中:

compile 'com.android.support:appcompat-v7:21.0.0'

Your activity_main.xmllayout would look something like that:

你的activity_main.xml布局看起来像这样:

<!--I use android:fitsSystemWindows because I am changing the color of the statusbar as well-->
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main_parent_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:fitsSystemWindows="true">

    <include layout="@layout/toolbar"/>

    <android.support.v4.widget.DrawerLayout
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <!-- Main layout -->
        <FrameLayout
            android:id="@+id/main_fragment_container"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

        <!-- Nav drawer -->
        <fragment
            android:id="@+id/fragment_drawer"
            android:name="com.example.packagename.DrawerFragment"
            android:layout_width="@dimen/drawer_width"
            android:layout_height="match_parent"
            android:layout_gravity="left|start" />
    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

Your Toolbar layout would look something like that:

您的工具栏布局看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    app:theme="@style/ThemeOverlay.AppCompat.ActionBar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="?attr/colorPrimary"/>

Your activity must extend from:

您的活动必须从:

ActionBarActivity

When you find your views (drawer and toolbar) in the activity the set the toolbar as the support action bar and set the setDrawerListener:

当您在活动中找到您的视图(抽屉和工具栏)时,将工具栏设置为支持操作栏并设置 setDrawerListener:

setSupportActionBar(mToolbar);
mDrawerToggle= new ActionBarDrawerToggle(this, mDrawerLayout,mToolbar, R.string.app_name, R.string.app_name);
mDrawerLayout.setDrawerListener(mDrawerToggle);

After that you just need to take care of the menu items and drawerToogle state:

之后,您只需要处理菜单项和 drawerToogle 状态:

 @Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = new MenuInflater(this);
    inflater.inflate(R.menu.menu_main,menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    mDrawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    mDrawerToggle.onConfigurationChanged(newConfig);
}

@Override
public void onBackPressed() {
    if(mDrawerLayout.isDrawerOpen(Gravity.START|Gravity.LEFT)){
        mDrawerLayout.closeDrawers();
        return;
    }
    super.onBackPressed();
}

The implementation is the same as It was before the Toolbar and you receive the arrow animation for free. No headaches. For more information follow:

实现与工具栏之前的相同,您可以免费获得箭头动画。没有头痛。更多信息请关注:

If you want to display the drawer over the Toolbar and under the status bar, please refer to this question.

如果要在工具栏上方和状态栏下方显示抽屉,请参阅此问题

EDIT:Use NavigationView from the support design library. Tutorial to learn how to use in here: http://antonioleiva.com/navigation-view/

编辑:使用支持设计库中的 NavigationView。在这里学习如何使用的教程:http: //antonioleiva.com/navigation-view/

回答by appbootup

回答by NeoKree

If you want the real navigation drawer with material design style (defined here)
I have implemented a custom library that do exactly that.
You can find it here

如果您想要具有材料设计风格(在此处定义)的真正导航抽屉,
我已经实现了一个自定义库,可以做到这一点。
你可以在这里找到

回答by Juan Mendez

Supporting top comment along with the new generated main_content's layout. I simply override the included content layout with DrawerLayout. Keep in mind that your drawerlayout must have this layout_behavior: appbar_scrolling_view_behavior

支持顶部评论以及新生成的 main_content 布局。我只是用 DrawerLayout 覆盖了包含的内容布局。请记住,您的抽屉布局必须具有以下 layout_behavior:appbar_scrolling_view_behavior

top container's layout https://github.com/juanmendez/jm_android_dev/blob/master/01.fragments/06.fragments_with_rx/app/src/main/res/layout/activity_recycler.xml#L17

顶部容器的布局 https://github.com/juanmendez/jm_android_dev/blob/master/01.fragments/06.fragments_with_rx/app/src/main/res/layout/activity_recycler.xml#L17

included content layout https://github.com/juanmendez/jm_android_dev/blob/master/01.fragments/06.fragments_with_rx/app/src/main/res/layout/content_recycler.xml#L9

包含内容布局 https://github.com/juanmendez/jm_android_dev/blob/master/01.fragments/06.fragments_with_rx/app/src/main/res/layout/content_recycler.xml#L9

see the navigation drawer!!

看导航抽屉!!