【问题标题】:Android Material Toolbar appearing behind fragmentAndroid Material Toolbar 出现在片段后面
【发布时间】:2023-03-29 06:30:01
【问题描述】:

我有一个应用程序,我需要一个底部栏作为我的主导航和标题的工具栏和一个设置图标。到目前为止,我所取得的成就是:

activity.xml

<?xml version="1.0" encoding="utf-8"?>

<layout 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">

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/Layout_Root"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <!--
        ********************************************************
        **** This is the app bar layout which contains the
        **** main Material Toolbar.
        ********************************************************
        -->

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/Layout_AppBar_Main"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        app:liftOnScroll="true">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/Toolbar_Main"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="@color/colorOnSurface"
            android:paddingStart="8dp"
            android:paddingEnd="8dp"
            app:layout_scrollFlags="scroll|enterAlways|snap" />
    </com.google.android.material.appbar.AppBarLayout>

    <fragment
        android:id="@+id/Navigation_Host"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_anchorGravity="bottom"
        app:layout_anchor="@id/Layout_AppBar_Main"
        app:defaultNavHost="true"
        app:navGraph="@navigation/navigation_main" />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/Bottom_Navigation_Main"
        styleBackground="@{true}"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:visibility="visible"
        app:menu="@menu/menu_bottom_navigation_main" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

</layout>

除了工具栏出现在片段后面之外,一切正常。工具栏只有在我将&lt;fragment&gt; 中的android:layout_height="match_parent" 更改为android:layout_height="wrap_content" 时才能正常工作,但我不希望这样,因为我需要我的片段来填充屏幕。有人可以帮忙吗?

【问题讨论】:

    标签: android android-toolbar android-coordinatorlayout android-appbarlayout coordinator-layout


    【解决方案1】:

    您忘记添加 app:layout_behavior="@string/appbar_scrolling_view_behavior

    做这样的事情。

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        >
    
        <fragment
            android:id="@+id/Navigation_Host"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_anchorGravity="bottom"
            app:layout_anchor="@id/Layout_AppBar_Main"
            app:defaultNavHost="true"
            app:navGraph="@navigation/navigation_main" />
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/Bottom_Navigation_Main"
            styleBackground="@{true}"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="bottom"
            android:visibility="visible"
            app:menu="@menu/menu_bottom_navigation_main" />
    </FrameLayout>
    

    【讨论】:

    • 这适用于工具栏和我的片段,但我的底部栏消失了
    • 它还在底部,你必须在xml视图中向上滚动。
    猜你喜欢
    • 2019-08-05
    • 2014-12-28
    • 1970-01-01
    • 1970-01-01
    • 2014-12-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-11-08
    相关资源
    最近更新 更多