- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
我们正在制作一个 Android 应用程序,并且有一些我们想要添加的东西。Gmail 应用的效果如何。
您可以选择要查看的帐户(应用程序的其余部分将相应地运行)。
编辑:
我现在已经有了一个(可用的)导航栏,但我想要的是标题中的圆形图标。我希望有人能够选择他们正在查看的用户。
最佳答案
用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 navigationdrawer, which holds the profile picture, name and email etc. You needto define this in a separate layout file we would look into that injust a moment.
Menu
This is the menu you want to show below your header, we definemenu in a menus folder, just like you define menu for your overflowmenu. So basically NavigationView is a container for the Header Viewand Menu which you are going to use in your sliding drawer. So nowthat you understand the NavigationView we can start building ourNavigation 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
上,您只需使用 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>
您还必须为要使用此 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 spinner (drop down list) example
- Android - Spinner
- Basic Spinner example (Stackoverflow question)
- Spinners (Android dev guide)
在您的标题 上进行设置后,处理项目选择并相应地更改用户配置文件。 (这最后一部分完全取决于您如何在您的应用程序上实现用户配置文件)。但作为一个良好的开端,您可以检查 android training site ,更具体地说,this part .
关于java - 像 Gmail 应用程序一样的 Android Studio 抽屉导航,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34768647/
我经常使用 SSMS 查询数据和构建数据集,我的 IT 部门负责数据库管理。 最近我发现了 Azure Data Studio,我喜欢: 智能感知 源代码控制(例如使用 Git) 来自社区的扩展 SQ
我想根据我使用的 visual studio 版本编译不同的东西,比如 #if VISUAL_STUDIO_VERSION > 2015 eventH?.Invoke(this, EventArgs.
我们的开发团队计划从 visual studio 2005 升级到 visual studio 2010 -- 跳过 visual studio 2008。 大部分项目是VB ASP.NET项目,使用
我的Visual Studio 2015无法构建2010平台工具集。它说: The build tools for Visual Studio 2010 (v100) cannot be found.
我目前正在使用 Visual Studio 2015 来编程 ASP.NET Core 应用程序。我对安装 Visual Studio 2017 有以下疑问: 什么被认为是最佳实践和/或最干净的方法?
尝试从扩展和更新获取 Visual Studio 扩展时,出现以下错误:- 向 visualstudiogallery.msdn.microsoft.com/Services/VStudio/Exte
这个问题在这里已经有了答案: Can Visual Studio Code and VS 2012 be installed on same computer? (1 个回答) 关闭去年。 在安装了
作为标准安装的一部分,Visual Studio Code 带有一个名为“Monokai Dimmed”的颜色主题。 有没有办法将它移植到 Visual Studio 2015?我检查了社区主题( h
我想开始编程 CUDA。 我已经安装了 Visual Studio 2010 Express。 我还安装了 nVidia nSight Visual Studio。 而且我具备所有常见的先决条件(Ne
Visual Studio Community Edition是否可以使用Visual Studio Online帐户上的存储库? 我一直为包含在Online帐户中的Visual Studio Onl
我有一个我一直在开发的应用程序,但在 android studio 上遇到了问题。当我点击“build->run”然后选择我的设备时,应用程序永远不会在我的手机上运行(并且自动出现的android-s
我正在使用Visual Studio2010。我面临的一个问题是,当我创建一个新的Web项目时,Visual Studio将创建该项目,并且不会在解决方案资源管理器中显示其解决方案。 另一件事是,我想
我通读了这里的许多帖子,却找不到一个有效的明确答案。因此,在花了一些时间使它生效之后,我认为应该发布它。 问题:发布配置文件将建立在服务器上,但不会发布。 解: 确保已安装Microsoft Wind
我正在尝试使用Visual Studio 2012构建针对.NET 3.5的C++ CLI应用程序。 通过安装Visual Studio 2008,并指定v90平台工具集,我已经在一台机器上进行了这项
我在 Microsoft Visual Studios 2013 中有一个项目,我想在 Microsoft Visual Studios 2010 中打开它。有什么简单的方法吗?还是我必须在2010年
我想知道,如果我发送一个解决方案文件夹(它是用 visual studio C# 编写的),您可以在 visual studio for mac 中打开解决方案吗? 在visual studio 20
有没有办法在 Visual Studio Code 和 Visual Studio 中设置相同的快捷方式(而不必每次都手动更改它们)? 例如,我在 Visual Studio Code 中经常使用 A
我刚开始了解 Visual Studio Code。我想知道,我可以将 Visual Studio 替换为所有 .NET 开发相关的工作吗? 我可以节省 Visual Studio 许可的成本吗? V
我安装了具有有效许可证(Visual Studio 订阅)的 Visual Studio 2019 企业版(VS 2019 16.1.4),它运行良好。 突然之间,当我尝试打开项目或项目中的任何文件时
我一直在使用 Compass 编译 Windows 环境中的 sass 文件,无论是在命令行上还是使用 Compass-app 来查看目录。 我刚刚开始使用 Visual Studio(专业版 201
我是一名优秀的程序员,十分优秀!