【问题标题】:android - Implement DrawerLayout with BottomNavigationView and FrameLayoutandroid - 使用 BottomNavigationView 和 FrameLayout 实现 DrawerLayout
【发布时间】:2020-06-29 10:55:22
【问题描述】:

我目前正在创建一个应用程序,它有一个 BottomNavigationView 和一个 FrameLayout 用于片段。不过,我也想实现 DrawerLayout 或 Hamburger Menu,正如this 视频中所说的

一个 DrawerLayout 应该只包含两个视图,一个用于 ActivityFragment,一个用于 NavigationView

是否可以在拥有 BottomNavigationView 的同时实现 DrawerLayout?如果是,怎么做?

这是我的activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/framelayoutFragment"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/bottomNavigationView"
        app:layout_constraintTop_toTopOf="parent" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="match_parent"
        android:layout_height="56dp"
        app:backgroundTint="@color/colorSecondaryDark"
        app:elevation="0dp"
        app:itemIconTint="@drawable/selector"
        app:itemTextColor="@drawable/selector"
        app:labelVisibilityMode="selected"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.5"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/menu_bottomnavgation" />

</androidx.constraintlayout.widget.ConstraintLayout>

【问题讨论】:

  • 整个&lt;ConstraintLayout&gt; 将是&lt;DrawerLayout&gt; 内的主要内容——即第一个孩子。您的抽屉 View - 例如,&lt;NavigationView&gt; - 将是 &lt;DrawerLayout&gt; 中的第二个孩子。

标签: android android-framelayout drawerlayout android-bottomnavigationview


【解决方案1】:

是的,你可以在拥有 BottomNavigationView 的同时实现 DrawerLayout,试试这个:

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:openDrawer="start"
    android:id="@+id/drawer_layout">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tool_bar"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <FrameLayout
            android:id="@+id/frame"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            app:layout_constraintBottom_toTopOf="@id/btm_nav"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@id/tool_bar" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/btm_nav"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/nav"
        android:background="#FFFFFF"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        />

</androidx.constraintlayout.widget.ConstraintLayout>

    <com.google.android.material.navigation.NavigationView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:id="@+id/nav_dr"
        app:menu="@menu/nav"
        android:layout_gravity="start"/>
</androidx.drawerlayout.widget.DrawerLayout>

【讨论】:

    猜你喜欢
    • 2018-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-11
    • 2022-07-14
    • 2018-07-12
    • 2018-02-17
    相关资源
    最近更新 更多