Java 类似 Gmail 应用程序的 Android Studio 导航抽屉

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34768647/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-11 15:58:00  来源:igfitidea点击:

Android Studio navigation drawer like Gmail app

javaandroiddesign-patternsnavigation-drawer

提问by Tvde1

We're making an android app, and there is something we want to add. Which is the effect the Gmail app has.

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

You can choose which account you want to view (and the rest of the app will behave accordingly).

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

Example

例子

EDIT:

编辑:

I now already have a (working) navigation bar, but the things I want are the round icons in the header. I want someone to be able to choose the user they are viewing.

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

采纳答案by Mauker

The effect you want can be achieved by using NavigationViewfrom the com.android.support:designsupport lib.

NavigationViewcom.android.support:design支持库中使用就可以达到你想要的效果。

You can find a full tutorial on that here. And you can download the full source code from that tutorial here.

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

And here's another nice tutorialthat you could follow.

这是您可以遵循的另一个不错的教程

But long story short, that view is split between two main parts, a header and a menu part, and each one of those you'll have to define on XML.

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

As from that tutorial:

从那个教程开始:

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 文件夹中定义菜单,就像您为溢出菜单定义菜单一样。所以基本上 NavigationView 是标题视图和菜单的容器,您将在滑动抽屉中使用它。因此,现在您了解了 NavigationView,我们可以开始构建我们的 Navigation Drawer。

With that in mind, build your header as you would do with any other layout. And the Menu is defined somewhat like the Toolbar/ActionBar menu. e.g.:

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

navigation_menu.xml

导航菜单.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>

Then, on your Activityyou'll just have to make a layout like the one found in the tutorial, using the DrawerLayoutalong with NavigationView.

然后,在你的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>

You'll also have to create some Fragmentsfor each screen you want to display with this NavigationView. After you've done that, on your Activityyou can handle the selection events by implementing NavigationView.OnNavigationItemSelectedListener, like this:

您还必须为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;
    }
}

As for your edit, the icons could be represented by an ImageView. And to navigate between multiple profiles, it depends on how you've implemented that logic on your app, but as a "generic" answer, you could switch those profiles using something like a Spinner.

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

Those tutorials will help you through that step:

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

Once you've set that up on your header, handle the item selection and change the user profile accordingly. (This last part depends ENTIRELY on how you've implemented user profiles on your app). But just as a head start, you could check the android training site, more specifically, this part.

一旦你在你的header上设置了它,处理项目选择并相应地更改用户配置文件。(这最后一部分完全取决于您如何在您的应用中实现用户配置文件)。但作为一个良好的开端,您可以查看android 培训网站,更具体地说,这部分

回答by IntelliJ Amiya

You should use NavigationView

你应该使用 NavigationView

It provides the framework for easy to implement material navigation drawer with the help of inflate navigation items through menu resource. Befor Navigation View, we have hard way to make material navigation drawer using listview or linearlayout with custom adapter, but now we just need to add Navigation View in DrawerLayout, everything else will be handled by Navigation View.

它提供了一个框架,可以通过菜单资源在导航项的帮助下轻松实现材质导航抽屉。在导航视图之前,我们很难使用带有自定义适配器的列表视图或线性布局来制作材质导航抽屉,但现在我们只需要在 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>

For this requirement You can check sample

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

  1. MaterialDrawer

  2. How To Make Material Design Navigation Drawer

  3. Playing with NavigationView

  1. 材料抽屉

  2. 如何制作 Material Design 导航抽屉

  3. 使用 NavigationView

Hope this helps .

希望这可以帮助 。

回答by Zhaolong Zhong

I think this MaterialDraweris what you're looking for. This library has a lot of examples. You can either use this library directly or read the source code and implement your own drawer.

我认为这个MaterialDrawer就是你要找的。这个库有很多例子。您可以直接使用这个库,也可以阅读源代码并实现自己的抽屉。

回答by Shreyas Patil

You can implement this Material Navigation drawer using MaterialNavigation library. Article about implementation is here.

您可以使用 MaterialNavigation 库实现这个 Material Navigation drawer。关于实现的文章在这里

You will just have to import that library and you're done. See demo code on below site:.

您只需导入该库即可完成。请参阅以下站点上的演示代码:。

https://github.com/PatilShreyas/MaterialNavigationView-Android

https://github.com/PatilShreyas/MaterialNavigationView-Android