首选项片段的 Android 嵌套图

问题描述 投票:0回答:0

我之前在这里问过这个问题,但我发布的其中一个代码块犯了一个错误。在这里再次供您参考

我有一个使用导航主机控制器的标准底部导航选择器。我想添加一个全屏的 PrefencesFragment。我的问题是,如果我使用 navcontroller,它将使用 navhost 片段,它下面有底部导航,这看起来很奇怪。

我知道我必须根据文档使用全球目的地 --> https://developer.android.com/guide/navigation/navigation-global-action

但是我如何让它替换整个布局而不仅仅是 navhost 片段?

我几乎希望我可以启动一个活动并将片段放在那里。那会是非常糟糕的风格吗?

这是我的布局

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

    <androidx.constraintlayout.widget.ConstraintLayout
        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">

        <com.google.android.material.appbar.AppBarLayout
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <com.google.android.material.appbar.MaterialToolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="?attr/colorPrimary"
                android:minHeight="?attr/actionBarSize"
                android:theme="?attr/actionBarTheme"
                app:logo="@drawable/baseline_post_add_24"
                app:logoDescription="Postering With Passion"
                app:title="Postering With Passion"
                app:titleCentered="true"
                app:titleTextColor="@color/white"
                tools:layout_editor_absoluteX="0dp"
                tools:layout_editor_absoluteY="0dp" >

            <ProgressBar
                android:id="@+id/toolbar_progress_bar"
                android:layout_width="36dp"
                android:layout_height="36dp"
                android:indeterminateTint="#cccccc"
                android:indeterminateTintMode="src_in"
                android:indeterminate="true"
                android:layout_marginRight="4dp"
                android:layout_gravity="right"
                android:visibility="visible"
                />

            </com.google.android.material.appbar.MaterialToolbar>

        </com.google.android.material.appbar.AppBarLayout>

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        app:layout_constraintBottom_toTopOf="@+id/bottom_navigation_view"
        app:layout_constraintTop_toTopOf="parent"
        android:layout_height="0dp"
        tools:context="com.plcoding.posterpalfeature.ui.activity.MainActivity">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/constraintLayout"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <fragment
                android:id="@+id/navHostFragment"
                android:name="androidx.navigation.fragment.NavHostFragment"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:defaultNavHost="true"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:navGraph="@navigation/job_nav_graph" />

        </androidx.constraintlayout.widget.ConstraintLayout>

        <include
            android:id="@+id/bottom_sheet"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@drawable/background_bottom_sheet"
            app:layout_anchorGravity="bottom"
            app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
            layout="@layout/layout_bottom_sheet"></include>


    </androidx.coordinatorlayout.widget.CoordinatorLayout>

        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/bottom_navigation_view"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:menu="@menu/bottom_navigation_menu"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:labelVisibilityMode="labeled"
            app:contentInsetEnd="0dp"
            app:contentInsetStart="0dp"/>

        </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

这是导航文件

<?xml version="1.0" encoding="utf-8"?>
<navigation 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/job_nav_graph"
    app:startDestination="@id/jobsListFragment">

        <fragment
            android:id="@+id/jobsListFragment"
            android:name="com.plcoding.posterpalfeature.ui.fragments.JobsListFragment"
            android:label="JobsListFragment"
            tools:layout="@layout/fragment_job_list">
            <action
                android:id="@+id/action_jobsListFragment_to_mapFragment"
                app:destination="@id/mapFragment"
                app:enterAnim="@anim/slide_in_left"
                app:exitAnim="@anim/slide_out_right"
                app:popEnterAnim="@anim/slide_in_left" />
            <action
                android:id="@+id/action_jobsListFragment_to_jobDetailFragment"
                app:destination="@id/jobDetailFragment"
                app:popUpTo="@id/jobsListFragment"
                app:popUpToInclusive="true" />
        </fragment>
        <fragment
            android:id="@+id/jobDetailFragment"
            android:name="com.plcoding.posterpalfeature.ui.fragments.JobDetailFragment"
            android:label="JobDetailFragment"
            tools:layout="@layout/fragment_job_detail">
            <argument
                android:name="jobId"
                android:defaultValue="1"
                app:argType="integer" />
        </fragment>
        <fragment
            android:id="@+id/mapFragment"
            android:name="com.plcoding.posterpalfeature.ui.fragments.MapFragment"
            android:label="MapFragment"
            tools:layout="@layout/fragment_map_detail" />
</navigation>

抱歉给您带来不便

android navigation android-navigation android-navigation-graph
© www.soinside.com 2019 - 2024. All rights reserved.