2016-01-13 206 views
10

我们正在制作一款Android应用,并且还有一些我们想要添加的内容。 这是Gmail应用程序具有的效果。Android Studio导航抽屉,如Gmail应用

您可以选择要查看哪个帐户(并且应用的其余部分将相应地执行)。

Example

编辑:

我现在已经有一个(工作)导航栏,但我想要的东西是在头一轮的图标。我希望有人能够选择他们正在查看的用户。

+0

检查最新的编辑,如果有帮助,不要忘记标记为已接受。 ;) – Mauker

+0

嘿,很抱歉打扰了,但你是怎么在右边加上“1”的? – Omer

+0

@Omer这是Gmail应用程序的屏幕截图。 – Tvde1

回答

24

您想要的效果可以通过使用com.android.support:design支持库中的NavigationView来实现。

你可以在here上找到完整的教程。您可以从该教程here下载完整的源代码。

here's another nice tutorial你可以按照。

但长话短说,该视图分为两个主要部分,一个标题和一个菜单部分,以及您必须在XML上定义的每个部分。

自该教程:

头查看

这种观点基本上是导航 抽屉,其持有的个人资料图片,姓名和电子邮件等,你需要 的顶部要在一个单独的布局文件中定义这个,我们只需要在 中查看一下。

菜单

这是你要显示你的标题下的菜单中,我们在菜单文件夹定义 菜单,就像你定义菜单为您溢出 菜单。所以基本上,NavigationView是一个容器,用于标题视图 和菜单,您将在滑动抽屉中使用它。所以现在你可以通过 了解NavigationView我们可以开始制作我们的 导航抽屉。

考虑到这一点,像使用其他布局一样构建标题。菜单的定义有点像工具栏/动作条菜单。例如: -

navigation_menu.xml

<?xml version="1.0" encoding="utf-8"?> 
<menu xmlns:android="http://schemas.android.com/apk/res/android"> 
    <group 
     android:checkableBehavior="single"> 

     <item 
      android:id="@+id/drawer_home" 
      android:checked="true" 
      android:icon="@drawable/icon_home" 
      android:title="@string/title_home"/> 

     <item 
      android:id="@+id/drawer_content" 
      android:icon="@drawable/icon_content" 
      android:title="@string/title_content"/> 

     <item 
      android:id="@+id/drawer_about" 
      android:icon="@drawable/icon_about" 
      android:title="@string/title_about"/> 

     <item 
      android:id="@+id/drawer_exit" 
      android:icon="@drawable/icon_exit" 
      android:title="@string/title_exit"/> 

     </group> 
</menu> 

然后,在你的Activity你就得做出那样的一个教程发现布局,使用与DrawerLayout一起NavigationView

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

    <LinearLayout 
     android:layout_height="match_parent" 
     android:layout_width="match_parent" 
     android:orientation="vertical" 
     > 
     <include 
      android:id="@+id/toolbar" 
      layout="@layout/tool_bar"/> 
     <FrameLayout 
      android:id="@+id/frame" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 

     </FrameLayout> 

    </LinearLayout> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation_view" 
     android:layout_height="match_parent" 
     android:layout_width="wrap_content" 
     android:layout_gravity="start" 
     app:headerLayout="@layout/header" 
     app:menu="@menu/navigation_menu"/> 
</android.support.v4.widget.DrawerLayout> 

您还可以创造一些Fragments为要使用此NavigationView显示每个屏幕。你这样做之后,你的Activity您可以通过实施NavigationView.OnNavigationItemSelectedListener,这样的处理选择事件:

public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener { 
    // Your Activity 
     @Override 
    public boolean onNavigationItemSelected(MenuItem menuItem) { 
     Fragment fragment = null; 

     switch(menuItem.getItemId()) { 
      case R.id.drawer_home: 
       fragment = new YourFragment(); 
       break; 
      case R.id.drawer_content: 
       fragment = new AnotherFragment(); 
       break; 
      case R.id.drawer_about: 
       fragment = new AboutFragment(); 
       break; 
      case R.id.drawer_exit: 
       // TODO - Prompt to exit. 
       finish(); 
       break; 
     } 

     if (fragment == null) { 
      fragment = new YourFragment(); 
     } 

     drawerLayout.closeDrawers(); 

     FragmentManager fragmentManager = getSupportFragmentManager(); 
      fragmentManager.beginTransaction() 
        .replace(R.id.container, fragment) 
        .commit(); 

     return true; 
    } 
} 

至于你的编辑,图标可以通过ImageView表示。并且要在多个配置文件之间导航,这取决于您如何在您的应用中实现该逻辑,但作为“通用”答案,您可以使用类似Spinner的内容来切换这些配置文件。

这些教程将帮助您完成这一步:

一旦你设置在您 ,处理t他选择项目并相应地更改用户配置文件。 (这最后一部分完全取决于您在应用中如何实现用户配置文件)。但是,如果要开始,可以检查android training site,更具体地说,是this part

+0

更完美的答案。前进#Mauker –

+0

@IntelliJAmiya什么?你能解释一下你的意思吗? – WarrenFaith

+0

我看到一篇博客文章,从Android Studio 1.4中剖析了NavigationView模板:http://androidessence.com/creating-a-material-design-navigation-drawer/ – AdamMc331

3

您应该使用NavigationView

它提供了一个框架,便于通过菜单来实现材料导航 抽屉充气导航项目的帮助下 资源。 Befor导航视图中,我们很难通过定制适配器 使用listview或linearlayout制作材质 抽屉,但现在我们只需要在DrawerLayout中添加导航视图, 其他所有内容都将由导航视图处理。

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

    <!-- Your contents --> 

    <android.support.design.widget.NavigationView 
     android:id="@+id/navigation" 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:layout_gravity="start" 
     app:menu="@menu/my_navigation_items" /> 
</android.support.v4.widget.DrawerLayout> 

对于这个要求,您可以检查样品

  1. MaterialDrawer

  2. How To Make Material Design Navigation Drawer

  3. Playing with NavigationView

希望这有助于。

0

我觉得这个MaterialDrawer是你在找什么。这个库有很多例子。您可以直接使用该库,也可以阅读源代码并实现自己的抽屉。