gpt4 book ai didi

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

转载 作者:搜寻专家 更新时间:2023-10-30 21:00:00 24 4
gpt4 key购买 nike

我们正在制作一个 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 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 上,您只需使用 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com