【问题标题】:Android, how to use coordinator layout with bottom navigation viewAndroid,如何在底部导航视图中使用协调器布局
【发布时间】:2021-05-20 11:01:36
【问题描述】:

我在使用带有底部导航栏的协调器布局时遇到问题,片段容器似乎位于底部导航栏下方。

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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/coordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

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

    <com.google.android.material.appbar.MaterialToolbar
        android:id="@+id/homeToolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize" />
</com.google.android.material.appbar.AppBarLayout>

<FrameLayout
    android:id="@+id/layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_anchorGravity="center"
    app:layout_behavior="@string/appbar_scrolling_view_behavior">

    <androidx.fragment.app.FragmentContainerView
        android:id="@+id/nav_host_fragment_activity_main"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:navGraph="@navigation/mobile_navigation" />

</FrameLayout>

<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
    app:menu="@menu/bottom_nav_menu" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

如何安排元素以在顶部应用栏和底部导航视图之间创建片段容器?

【问题讨论】:

    标签: android android-coordinatorlayout bottomnavigationview


    【解决方案1】:

    LinearLayout 包裹整个布局并将其属性方向设置为vertical。比将 AppBarBottomNavigation 移到 CoordinatorLayout 之外。

    您的小部件树应该是这样的:

    <LinearLayout>
        <Appbar/>
        <CoordinatorLayout/>
        <BottomNavigation/>
    </LinearLayout>
    

    【讨论】:

      【解决方案2】:

      您可以使用 ConstraintLayout 代替 CoordinatorLayout。

      <?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:id="@+id/coordinatorLayout"
          android:layout_width="match_parent"
          android:layout_height="match_parent"
          android:fitsSystemWindows="true">
      
          <com.google.android.material.appbar.AppBarLayout
              android:id="@+id/appBarLayout"
              android:layout_width="0dp"
              android:layout_height="wrap_content"
              app:layout_anchorGravity="top"
              app:layout_constraintEnd_toEndOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              app:liftOnScroll="true">
      
              <com.google.android.material.appbar.MaterialToolbar
                  android:id="@+id/homeToolbar"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content" />
          </com.google.android.material.appbar.AppBarLayout>
      
      <FrameLayout
          android:layout_width="match_parent"
          android:layout_height="0dp"
          app:layout_constraintTop_toBottomOf="@id/appBarLayout"
          app:layout_constraintBottom_toTopOf="@id/nav_view"/>
         <androidx.fragment.app.FragmentContainerView
              android:id="@+id/nav_host_fragment_activity_main"
              android:name="androidx.navigation.fragment.NavHostFragment"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              app:defaultNavHost="true"
              app:navGraph="@navigation/mobile_navigation" />
      
      
          <com.google.android.material.bottomnavigation.BottomNavigationView
              android:id="@+id/nav_view"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              app:layout_behavior="@string/hide_bottom_view_on_scroll_behavior"
              app:layout_constraintBottom_toBottomOf="parent"
              app:layout_constraintStart_toStartOf="parent"
              app:menu="@menu/bottom_nav_menu" />
      
      </androidx.constraintlayout.widget.ConstraintLayout>
      

      【讨论】:

      • 需要CoordinatorLayout来协调顶部应用栏和滚动内容。
      • 我认为最好的方法可能是实现一个自定义行为来处理底部锚定,有人知道从哪里开始吗?
      猜你喜欢
      • 2018-12-05
      • 1970-01-01
      • 1970-01-01
      • 2017-09-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多