如何使用Android设计支持库创建导航抽屉? [英] How to Create Navigation Drawer Using Android Design Support Library?

查看:77
本文介绍了如何使用Android设计支持库创建导航抽屉?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google开发人员博客文章中.我了解了使用名为

In google developer blog post . I read about new way to create navigation drawer using new dependency called

compile 'com.android.support:design:22.2.0'

但是我没有找到使用此新依赖项创建导航抽屉的确切方法.

but I didn’t found exact way to create navigation drawer using this new dependency.

在build.gradle中,我添加了依赖项

In build.gradle , I have added dependency

compile 'com.android.support:design:22.2.0'
compile 'com.android.support:appcompat-v7:22.0.0'

在布局文件中添加了以下代码(基于Google博客文章)

in layout file added following code (based on google blog post)

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

    <!-- your content layout -->

    <android.support.design.widget.NavigationView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            app:headerLayout="@layout/drawer_header"
            app:menu="@menu/drawer"/>
</android.support.v4.widget.DrawerLayout>

我还尝试过使用ActionBarActivity扩展类,但是不赞成使用它吗?

Aslo I tried to extend class using ActionBarActivity but its deprecated?

参考: http://android-developers. blogspot.in/2015/05/android-design-support-library.html

任何帮助表示感谢.谢谢

Any help appreciated .Thank you

推荐答案

尝试执行以下步骤

添加Android设计支持库依赖项

Add Android design support library dependency

compile 'com.android.support:design:22.2.1'

为导航抽屉创建标题

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="150dp"
    android:background="@drawable/header"
    android:padding="16dp"
    android:orientation="vertical"
    android:gravity="bottom">

    <TextView
        android:textColor="#ffffff"
        android:textSize="18sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Nirav Kalola\nnkDroid"
        />
    </LinearLayout>

为导航抽屉项目创建菜单

Create a menu for navigation drawer items

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="nkdroid.tutorial.navigationview.MainActivity">
<group android:checkableBehavior="single">
    <item
        android:id="@+id/navigation_item_1"
        android:icon="@drawable/ic_action_home"
        android:title="Home"/>
    <item
        android:id="@+id/navigation_item_2"
        android:icon="@drawable/ic_action_info"
        android:title="About Us"/>
    <item
        android:id="@+id/navigation_subheader"
        android:title="Tutorials">
        <menu>
            <item
                android:id="@+id/navigation_sub_item_1"
                android:icon="@drawable/ic_image_looks_one"
                android:title="Android Tutorials"/>
            <item
                android:id="@+id/navigation_sub_item_2"
                android:icon="@drawable/ic_image_looks_two"
                android:title="IOS Tutorials"/>
        </menu>
    </item>
</group>
</menu>

使用标题和项目创建导航视图

Create Navigation View with header and items

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

<!--Main content-->
<LinearLayout
    android:orientation="vertical"
    android:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <include layout="@layout/app_bar"/>
</LinearLayout>

<!--Navigation Drawer-->
<android.support.design.widget.NavigationView
    android:id="@+id/main_drawer"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"

    app:headerLayout="@layout/drawer_header"
    app:menu="@menu/menu_drawer"/>
</android.support.v4.widget.DrawerLayout>

实施导航视图

import android.content.res.Configuration;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;

public class MainActivity extends AppCompatActivity implements    NavigationView.OnNavigationItemSelectedListener{

private Toolbar toolbar;
private NavigationView mDrawer;
private DrawerLayout mDrawerLayout;
private  ActionBarDrawerToggle drawerToggle;
private int mSelectedId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    setToolbar();
    initView();

    drawerToggle=new ActionBarDrawerToggle(this,mDrawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
    mDrawerLayout.setDrawerListener(drawerToggle);
    drawerToggle.syncState();
    //default it set first item as selected
    mSelectedId=savedInstanceState ==null ? R.id.navigation_item_1: savedInstanceState.getInt("SELECTED_ID");
    itemSelection(mSelectedId);

}

private void setToolbar() {
    toolbar= (Toolbar) findViewById(R.id.toolbar);
    if (toolbar != null) {
        setSupportActionBar(toolbar);
    }
}

private void initView() {
    mDrawer= (NavigationView) findViewById(R.id.main_drawer);
    mDrawer.setNavigationItemSelectedListener(this);
    mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout);
}

private void itemSelection(int mSelectedId) {

    switch(mSelectedId){

        case R.id.navigation_item_1:
            mDrawerLayout.closeDrawer(GravityCompat.START);
            break;

        case R.id.navigation_item_2:
            mDrawerLayout.closeDrawer(GravityCompat.START);
            break;

        case R.id.navigation_sub_item_1:
            mDrawerLayout.closeDrawer(GravityCompat.START);
            break;

        case R.id.navigation_sub_item_2:
            mDrawerLayout.closeDrawer(GravityCompat.START);
            break;

    }

}

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

@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
    menuItem.setChecked(true);
    mSelectedId=menuItem.getItemId();
    itemSelection(mSelectedId);
    return true;
}

@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
    super.onSaveInstanceState(outState, outPersistentState);
    //save selected item so it will remains same even after orientation change
    outState.putInt("SELECTED_ID",mSelectedId);
}

}

您可以直接从我的博客

you can directly download source code from my blog

这篇关于如何使用Android设计支持库创建导航抽屉?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆