Android Studio导航抽屉,如Gmail应用

Tvd*_*de1 12 java android design-patterns navigation-drawer

我们正在制作一个Android应用程序,我们想要添加一些东西.这是Gmail应用程序的效果.

您可以选择要查看的帐户(应用程序的其余部分也会相应地运行).

例

编辑:

我现在已经有了一个(工作)导航栏,但我想要的是标题中的圆形图标.我希望有人能够选择他们正在查看的用户.

Mau*_*ker 25

你想要的效果可以通过使用可以实现NavigationViewcom.android.support:design支持库.

你可以在这里找到完整的教程.您可以在此处下载该教程的完整源代码.

这里的另一个很好的教程,你可以遵循.

但简而言之,该视图分为两个主要部分:标题和菜单部分,以及您必须在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>
Run Code Online (Sandbox Code Playgroud)

然后,在你的工作中Activity你只需要制作一个类似教程中的布局,使用DrawerLayoutwith 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>
Run Code Online (Sandbox Code Playgroud)

您还必须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;
    }
}
Run Code Online (Sandbox Code Playgroud)

至于你的编辑,图标可以用一个表示ImageView.要在多个配置文件之间导航,这取决于您在应用程序上实现该逻辑的方式,但作为"通用"答案,您可以使用类似的方式切换这些配置文件Spinner.

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

标题上设置后,处理项目选择并相应地更改用户配置文件.(最后一部分完全取决于您如何在应用程序上实现用户配置文件).但是,作为一个先发优势,你可以检查Android培训网站,更具体地说,这部分.