java - 像 Gmail 应用程序一样的 Android Studio 抽屉导航

标签 java android design-patterns navigation-drawer

我们正在制作一个 Android 应用程序,并且有一些我们想要添加的东西。 Gmail 应用的效果如何。

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

Example

编辑:

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

最佳答案

NavigationView就可以达到你想要的效果来自 com.android.support:design 支持库。

您可以找到有关该内容的完整教程 here .您可以从该教程下载完整的源代码 here .

here's another nice tutorial你可以跟随。

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

从那个教程开始:

Header View

This View is basically the top part of the navigation drawer, which holds the profile picture, name and email etc. You need to define this in a separate layout file we would look into that in just a moment.

Menu

This is the menu you want to show below your header, we define menu in a menus folder, just like you define menu for your overflow menu. So basically NavigationView is a container for the Header View and Menu which you are going to use in your sliding drawer. So now that you understand the NavigationView we can start building our Navigation Drawer.

考虑到这一点,像处理任何其他布局一样构建标题。 Menu 的定义有点像 Toolbar/ActionBar 菜单。例如:

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 上,您只需使用 DrawerLayoutNavigationView.

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

您还必须为要使用此 NavigationView 显示的每个屏幕创建一些 Fragments。完成后,在您的 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.frame, fragment)
                    .commit();

        return true;
    }
}

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

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

在您的标题 上进行设置后,处理项目选择并相应地更改用户配置文件。 (这最后一部分完全取决于您如何在您的应用程序上实现用户配置文件)。但作为一个良好的开端,您可以检查 android training site ,更具体地说,this part .

关于java - 像 Gmail 应用程序一样的 Android Studio 抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34768647/

相关文章:

java - 日期解析魔法

Android WebView,错误 net::Name_Not_Resolved

c# - 有没有一种方法可以使用一种方法来处理其他方法以避免代码重复?

java - 在没有 Activity 的另一个类文件中使用 Toast 扩展了 Android

java - 如果不在数据库中,Android Studio SQLite 会保存标题吗?

design-patterns - 层、管道和过滤器之间的区别?

java - 如何创建包含集合的不可变类的不可变构建器?

java - 如何在运行时提取 Selenium Web 元素的识别策略?

java - 如何将 Libgdx 游戏部署到 iOS

java - 如何动态选择向导的下一页