Вид навигации не работает и занимает весь экран

#android #android-studio #android-layout #android-fragments

#Android #android-studio #android-макет #android-фрагменты

Вопрос:

 <?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"
android:id="@ id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.project.noticeboard.mainNoticeBoard">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.appcompat.widget.Toolbar
        android:id="@ id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="#03A9F4"
        android:elevation="5dp"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <FrameLayout
        android:id="@ id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</LinearLayout>
<com.google.android.material.navigation.NavigationView
    android:id="@ id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:headerLayout="@layout/header_navbar"
    app:menu="@menu/navbar_main"
    android:layout_marginRight="200dp"/>


</androidx.drawerlayout.widget.DrawerLayout>
 

Я пытаюсь создать панель навигации, но она охватывает весь экран и не реагирует на экран.
это макет
Я полагаю, что за ним стоит фрагмент, но по умолчанию навигация открыта.

Ответ №1:

Я думаю, вы добавляете свои фрагменты в «fragment_container«, который представляет собой FrameLayout внутри LinearLayout вместо nav_view. Я также наблюдаю, что вы используете match_parent для высоты nav_view. Который будет перекрывать ваш fragment_container. Лучше создать свой DrawerLayout, как показано ниже, который рекомендуется разработчиками Android.

 <?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:id="@ id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<com.google.android.material.navigation.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" />

</androidx.drawerlayout.widget.DrawerLayout>
 

Комментарии:

1. не работает, по-прежнему охватывает весь экран, если я сделаю, как вы говорите

2. Можете ли вы ввести код ur и предоставить ссылку на GitHub, если это возможно? Я могу разобраться в этом.

3. это сработало, я просто удалил линейную компоновку, чтобы проверить разницу, а затем снова добавил i на то же место, и внезапно это сработало даже без изменения одной строки.

Ответ №2:

Добавить android:layout_gravity="start" в свой NavigationView . Это переместит его за пределы экрана, пока вы не проведете пальцем вправо, чтобы открыть его.