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

时间:2018-07-14 06:58:37

标签: android android-coordinatorlayout bottomnavigationview

我想在滚动时隐藏我的bottomNavigationView

这是我的活动

<?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无法转换为   android.support.design.widget.CoordinatorLayout $ LayoutParams

更新: 当我将BottomNavigationView直接放在CoordinatorLayout内时,我的视图改变了并且不好: enter image description here

UPDATE2: 我通过更改一些attrb来解决此问题:

<?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>

2 个答案:

答案 0 :(得分:1)

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

您的BottomNavigationView的父母应为CoordinatorLayout。因此,请尝试将BottomNavigationView直接放在CoordinatorLayout

答案 1 :(得分:1)

尝试一下:

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