簡體   English   中英

適用於KitKat的Android 5.0材質設計風格導航抽屜

[英]Android 5.0 material design style navigation drawer for KitKat

我看到Android推出了新的導航抽屜圖標,抽屜圖標和后退箭頭圖標。 我們如何在Kitkat支持的應用程序中使用它。 查看Google最新版的報亭應用,它具有最新的導航抽屜圖標和動畫。 我們怎樣才能實現呢?

我已經嘗試將minSDK設置為19並將complileSDK設置為21,但它使用的是舊式圖標。 這是自我實施的嗎?

您需要使用appcompat v21中的新工具欄以及此庫中的新ActionBarDrawerToggle

將gradle依賴項添加到gradle文件:

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

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

您的工具欄布局看起來像這樣:

<?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"/>

您的活動必須延伸至:

ActionBarActivity

當您在活動中找到視圖(抽屜和工具欄)時,將工具欄設置為支持操作欄並設置setDrawerListener:

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

之后你只需要處理菜單項和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();
}

實現與工具欄之前的實現相同,您可以免費獲得箭頭動畫。 沒有頭疼。 有關更多信息,請訪

如果要在工具欄上方和狀態欄下方顯示抽屜,請參閱此問題

編輯:使用支持設計庫中的NavigationView。 在這里學習如何使用的教程: http//antonioleiva.com/navigation-view/

如果您想要具有材料設計風格的真實導航抽屜( 在此處定義)
我已經實現了一個完全相同的自定義庫。
你可以在這里找到它

支持熱門評論以及新生成的main_content布局。 我只是使用DrawerLayout覆蓋包含的內容布局。 請記住,drawerlayoutout必須具有此layout_behavior:appbar_scrolling_view_behavior

頂部容器的布局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/content_recycler.xml#L9

看導航抽屜!!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM