将抽屉布局添加到主要活动中

2024-05-04

我创建了一个应用程序。它工作得很好,但现在我想在主活动中包含导航抽屉。我知道代码模板,但为此我需要创建新活动。我的问题是如何将抽屉布局包含到现有活动中仅包含 Recycler 视图和 fab 按钮,是否可以包含?抱歉这个大问题。


您可以按照以下步骤逐步添加NavigationDrawer:

1)将您的活动父布局设置为抽屉布局:

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

2) Add 导航视图在 DrawerLayout 的底部:

<android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />

</android.support.v4.widget.DrawerLayout>

3)在res文件夹下的menu文件夹中添加一个名为的xml文件活动_主_抽屉.xml对于菜单项:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group
        android:id="@+id/top"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_camera"
            android:icon="@drawable/ic_menu_camera"
            android:title="Home" />
    </group>

    <group
        android:id="@+id/middle"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_share"
            android:icon="@drawable/ic_menu_share"
            android:title="Directory" />
    </group>

    <group
        android:id="@+id/bottom"
        android:checkableBehavior="single">
        <item
            android:id="@+id/nav_send"
            android:icon="@drawable/ic_menu_send"
            android:title="About Us" />
    </group>

</menu>

4)实施NavigationView.OnNavigationItemSelectedListener在您的活动中:

public class YourActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
}

5) Add onNavigationItemSelected()方法来定义对所选导航项的操作。

 @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }

6) 配置 DrawerLayout 并在 Activity 的 onCreate() 中为您的 NavigationView 设置侦听器:

  DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
  ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
  drawer.setDrawerListener(toggle);
  toggle.syncState();

  NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
  navigationView.setNavigationItemSelectedListener(this);

7) 将 nav_header_main.xml 添加到布局文件夹中;

   <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="@dimen/nav_header_height"
    android:background="@drawable/side_nav_bar"
    android:gravity="bottom"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:src="@android:drawable/sym_def_app_icon" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingTop="@dimen/nav_header_vertical_spacing"
        android:text="Android Studio"
        android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="[email protected] /cdn-cgi/l/email-protection" />

</LinearLayout>

如果要添加工具栏,请将以下代码粘贴到 DrawerLayout 的顶部:

<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:background="?attr/colorPrimary"
                app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.AppBarLayout>

并在 NavigationView 之前关闭 CoordinatorLayout:

</android.support.design.widget.CoordinatorLayout>

让我知道它是如何工作的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

将抽屉布局添加到主要活动中 的相关文章

  • 为什么 Android 服务在测试时不是单例的?

    当运行大量测试套件时 我注意到我的一个 Android 服务不再是单例的 Android 服务应该是单例的 但是当使用 ServiceTestCase 时 我的引用计数超过 1 在 onCreate 中递增 在 onDestroy 中递减
  • SQL Server 2008R2 和创建 XML 文档

    论坛上的第一篇文章 因为我真的被这个问题困住了 以下查询正确地将有效的 XML 文档分配给 xTempXML 变量 类型为 xml 注 文档的长度 转换为varchar max 711 select xTempXML select Pres
  • 自定义视图的Ondraw无限循环android

    我的自定义视图的 OnDraw 函数被无限调用并且正在循环 可能的原因是什么 这是我的自定义视图 public class Balls extends View private static final String TAG BallsVi
  • Kotlin - 即使有 init() 函数,属性也必须初始化或者是抽象的

    我已使用 Android Studio 3 0 将此代码从 Java 转换为 Kotlin internal var background Drawable internal var xMark Drawable private fun i
  • 请求超级用户权限编辑文件

    我正在规划一个需要编辑系统文件的应用程序 我只能使用 root 权限编辑该文件 我有一个已 root 且安装了 Superuser apk 的开发手机 其他需要 root 的应用程序会在首次启动时请求 root 访问权限 我想做同样的事情
  • 如何修复 Android 7.0 的 Spinner 模式下的 DatePickerDialog?

    我目前正在开发一个简单的项目 其中包含一个包含在 Web 视图中的网站 具有少量交互 以提高网站本身和 Android 移动设备之间的交互性 由于该网站包含用户生日的日期输入字段 因此我希望实现一个与所有设备兼容的旋转格式的日期选择器 我尝
  • 屏幕滚动时 GridView 内的项目会重复

    我使用 GridView 来显示一组用户可以选择的类别 网格的每个项目都由一个 ImageView 和一个 TextView 组成 两者都是从服务器检索的 当触摸一个项目时 另一个活动就会启动 我以为一切都很顺利 直到我注意到当我滚动屏幕时
  • 输入连接-如何删除选定的文本?

    我为 Android 制作了一个自定义键盘 当我按下键盘的退格按钮时 我使用 getCurrentInputConnection deleteSurroundingText 1 0 从输入字段中删除一个字母 但是 当我选择一些文本然后按退格
  • 关闭 XDOCUMENT 的实例

    我收到这个错误 该进程无法访问文件 C test Person xml 因为它是 被另一个进程使用 IOException 未处理 保存文件内容后如何关闭 xml 文件的实例 using System using System Collec
  • 从代码动态更改多个文本视图的大小(没有“磁盘上”xml 主题)?

    我有 10 个文本视图在我的代码中 我想更改所有代码的字体大小 在我的布局中我使用了 style定义通用属性 但是我不知道一旦布局出现在屏幕上如何从代码中更改它们 我不想做的是更新 AND 对象 但只写在一处 我知道我可以使用应用主题但这假
  • Facebook Android 意图

    我对这个意图有疑问 这个意图是发送文本类型的消息 一切正常 电子邮件 短信 推特以及手机上的任何内容 但唯一有问题的是facebook 它会尝试以链接而不是文本的形式发布 Intent s new Intent android conten
  • 点击当前选项卡刷新页面时的 Xamarin.Forms TabbedPage 事件

    我正在使用 Xamarin Forms 构建 iOS Android 应用程序 并有一个 TabbedPage 如果用户已经在选项卡 2 上 并且单击了选项卡 2 并且我希望刷新选项卡 2 或者运行我自己的函数 以便我可以自己刷新它 有没有
  • 如何使用云打印打印Android活动显示

    我正在尝试将 Google 云打印实现到应用程序中 遵循集成指南 https developers google com cloud print docs android 我试图通过打印 google com 来保持基本 单击我创建的打印按
  • 带动画的 ScrollTo(0,250) Android ScrollView

    当我滚动到 0 250 时 我想在滚动动作中包含一个动画 我做了这段代码 但它没有根据动画滚动 scrollMe 是滚动小部件 id ObjectAnimator anim ObjectAnimator ofInt scrollMe tra
  • 如何在kotlin中使用Coroutine每秒调用一个函数

    我刚刚创建了一个应用程序 其中我的函数 getdata 每秒调用一次以从服务器获取新数据 而 updateui 函数将更新 UI 中的视图 我在我的应用程序中不使用任何异步任务或协程 我想这样做 请告诉我我怎样才能做到这一点 这是我的代码
  • 如何使用百分比进行android布局?

    我们如何使用百分比android视图元素的值 像这样的东西
  • 使用 qbs 构建 qt 应用程序

    我想知道在 Linux 上使用 qbs 编译 构建和创建 Android Qt 应用程序的步骤 我拥有所有必要的工具 目前我可以使用 qmake 创建 apk Qbs 目前不支持构建 Qt Android 应用程序 Qbs v1 4 中引入
  • Google Place Api:来自此 Android 客户端应用程序 com.package.name 的请求被阻止

    我在用PlaceAutocompleteFragment当我单击搜索字段 PlaceAutocompleteFragment 对话框消失时 我收到此错误 errors domain global re ason forbidden mess
  • 读/写带有特殊字符的.txt文件

    I open Notepad Windows 并写 Some lines with special characters Special 并前往另存为 someFile txt 与Encoding set to UTF 8 在Java中我有
  • 如何减少导航图标和工具栏标题之间​​的差距?

    我的问题是导航抽屉图标和工具栏标题之间 有多余的空间 示例图像如下 工具栏的xml视图是

随机推荐