尝试获取“BottomNavigationView”的布局参数时应用程序崩溃

App crashes when try to get `BottomNavigationView`'s layout params

我想在滚动时隐藏我的 bottomNavigationView

这是我的 activity:

<?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"
    tools:context=".CircularActivity">
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_above="@+id/bottomBar">
            <include layout="@layout/content_main" />
        </LinearLayout>
        <RelativeLayout
            android:id="@+id/bottomBar"
            android:layout_width="match_parent"
            android:layout_height="50dp"
            android:layout_alignParentBottom="true">
            <android.support.design.widget.BottomNavigationView
                android:id="@+id/bottomNavView_Bar"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_gravity="bottom"
                android:background="?android:attr/windowBackground"
                android:foreground="?attr/selectableItemBackground"
                app:itemBackground="@color/bgBottomNavigation"
                app:itemIconTint="@android:color/white"
                app:itemTextColor="@android:color/white"
                app:menu="@menu/navigation">
            </android.support.design.widget.BottomNavigationView>
        </RelativeLayout>
    </RelativeLayout>
</android.support.design.widget.CoordinatorLayout>

我写了 BottomNavigationViewHelper 但是当我设置行为时:

CoordinatorLayout.LayoutParams layoutParams = (CoordinatorLayout.LayoutParams) bottomNavigationView.getLayoutParams();
layoutParams.setBehavior(new BottomNavigationViewHelper());

我收到这个错误:

android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.design.widget.CoordinatorLayout$LayoutParams

更新: 当我将 BottomNavigationView 直接放在 CoordinatorLayout 中时,我的视图发生变化并且效果不佳:

更新 2: 我通过更改一些属性来修复:

<?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"
    tools:context=".CircularActivity">
    <include layout="@layout/content_main"/>
    <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavView_Bar"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:layout_gravity="bottom"
        android:background="?android:attr/windowBackground"
        android:foreground="?attr/selectableItemBackground"
        app:itemBackground="@color/bgBottomNavigation"
        app:itemIconTint="@android:color/white"
        app:itemTextColor="@android:color/white"
        app:menu="@menu/navigation">
    </android.support.design.widget.BottomNavigationView>
</android.support.design.widget.CoordinatorLayout>

问题是您尝试将 RelativeLayout.LayoutParams 转换为 CoordinatorLayout.LayoutParams

你的 BottomNavigationView 的 parent 应该是 CoordinatorLayout。所以尝试把 BottomNavigationView 直接放在 CoordinatorLayout

里面

试试这个:

RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) bottomNavigationView.getLayoutParams();
layoutParams.setBehavior(new BottomNavigationViewHelper());