【问题标题】:How to show BottomNavigation CoordinatorLayout in Android如何在 Android 中显示 BottomNavigation CoordinatorLayout
【发布时间】:2017-06-09 16:45:12
【问题描述】:

在我的应用程序中,我想显示CoordinatorLayout 的底部BottomNavigation,为此我编写以下代码:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
   >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:fitsSystemWindows="true"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main.collapsing"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:layout_scrollFlags="scroll|exitUntilCollapsed">


            <include
                android:id="@+id/mainToolbar"
                layout="@layout/toolbar_main" />

        </android.support.design.widget.CollapsingToolbarLayout>

    </android.support.design.widget.AppBarLayout>


    <com.aurelhubert.ahbottomnavigation.AHBottomNavigationViewPager
        android:id="@+id/mainViewPager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/mainBottomNavigation"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />

    <com.aurelhubert.ahbottomnavigation.AHBottomNavigation
        android:id="@+id/mainBottomNavigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:layout_anchorGravity="bottom"
        app:selectedBackgroundVisible="true" />

</android.support.design.widget.CoordinatorLayout>

但是当运行应用程序时显示BottomNavigation top of CoordinatorLayout

如何在CoordinatorLayout 的底部显示BottomNavigation

【问题讨论】:

  • 你的bottomnavigationmatch_parent属性android:layout_height="match_parent"尝试使用wrap_content代替。
  • @IbrahimAli,我想在布局底部显示这个 AHBottomNavigation

标签: android android-coordinatorlayout bottomnavigationview


【解决方案1】:

我希望答案还不算太晚。我刚刚遇到了同样的问题,我使用了 android:layout_gravity="bottom"。 我有一个 Toolbar、一个 BottomNavigationView,在中间,我有一个 FrameLayout,用作片段的占位符。这是我的 XML 布局:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:background="@drawable/bg_main"
  android:minHeight="?attr/actionBarSize">

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/colorTab"
    app:layout_scrollFlags="scroll|enterAlways"
    />
</android.support.design.widget.AppBarLayout>

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

    <android.support.v4.view.ViewPager
        android:id="@+id/slide_viewpager"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</FrameLayout>

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:itemBackground="@color/colorTab"
    app:itemIconTint="@drawable/bottom_navigation_toolbar"
    app:itemTextColor="@drawable/bottom_navigation_toolbar"
    app:menu="@menu/bottom_bar"
    />

</android.support.design.widget.CoordinatorLayout>

另请查看this 问题,它使用几乎相同的布局,并且还显示了如何更改 BottomNavigationView 的行为,以便在滚动时隐藏。如果您希望实现该功能,请确保创建类 BottomNavigationBehavior(或任何您想调用的名称)并将这一行添加到 XML 中的 BottomNavigationView:

app:layout_behavior="com.yourpackage.yourpackage.BottomNavigationBehavior"

希望对你有帮助!

【讨论】:

  • 这会导致FrameLayout过大,继续在BottomNavigationView后面?
  • @A1m 你必须检查一下,我相信不是,但你可以随时调整它的大小。
  • 我的问题是对于某些视图 BottomNavigationViewgone ...所以我需要编写特殊代码来处理它,除非布局处理它。 (这里没有)
  • @A1m 嗯,我有一个类似的设置,在某些情况下我隐藏了BottomNavigationView。刚看了我的代码,布局确实在视图后面继续,但我只是设置了底部边距来解释BottomNavigationView
猜你喜欢
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-01-18
  • 1970-01-01
  • 2021-02-16
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多