当前位置: 移动技术网 > IT编程>开发语言>Java > 记一次BottomNavigationView使用踩坑

记一次BottomNavigationView使用踩坑

2020年07月17日  | 移动技术网IT编程  | 我要评论
错误信息:1.2020-07-16 14:47:36.848 6032-6032/com.example.kotlinprj E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.kotlinprj, PID: 6032java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kotlinprj/com.example.kotli...

错误信息:

1.2020-07-16 14:47:36.848 6032-6032/com.example.kotlinprj E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.kotlinprj, PID: 6032java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.kotlinprj/com.example.kotlinprj.MainActivity}: android.view.InflateException: Binary XML file line #28 in com.example.kotlinprj:layout/activity_main: Binary XML file line #28 in com.example.kotlinprj:layout/activity_main: Error inflating class com.google.android.material.bottomnavigation.BottomNavigationView

2.2020-07-16 14:47:36.848 6032-6032/com.example.kotlinprj E/AndroidRuntime: Caused by: java.lang.IllegalArgumentException: Cannot set 'scaleX' to Float.NaN

 

这是我的layout_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity"
        android:background="@color/MyBgColor"
        >

    <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:animateLayoutChanges="true"
            android:orientation="vertical">
        <FrameLayout
                android:id="@+id/nav_host_container"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_weight="1"
                />

       
        <com.google.android.material.bottomnavigation.BottomNavigationView
                android:id="@+id/bottom_nav"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                style="@style/Widget.Design.BottomNavigationView"
                android:layout_gravity="bottom"
                android:background="#fff"
                android:elevation="8dp"
                app:labelVisibilityMode="labeled"
                app:itemIconSize="20dp"
                app:menu="@menu/bottom_nav_menu"/>

    </LinearLayout>

    <ImageView
            android:id="@+id/iv_add"
            android:layout_width="70dp"
            android:layout_height="70dp"
            android:layout_gravity="center_horizontal"
            android:layout_marginBottom="5dp"
            android:src="@mipmap/img_nav_add"
            android:layout_centerInParent="true"
            android:layout_alignParentBottom="true" />


</RelativeLayout>
bottom_nav_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:id="@+id/home"
        android:icon="@mipmap/icon_home_normal"
        android:title="@string/title_home" />

    <item android:id="@+id/shop"
        android:title="@string/title_shop"
        android:icon="@mipmap/icon_shop_normal" />

    <item android:id="@+id/add"
        android:title="@string/title_add"
        android:icon="@mipmap/icon_add_normal" />

    <item android:id="@+id/message"
        android:title="@string/title_message"
        android:icon="@mipmap/icon_message_normal" />

    <item android:id="@+id/my"
        android:title="@string/title_my"
        android:icon="@mipmap/icon_my_normal" />

</menu>

我要实现的底部导航成功后如下图

kotlinprj_add_index

设计给出的需求是底部导航只显示icon不需要显示title,且中间的导航按钮是比其他几按钮个大的,按钮选中也不需要动画,只需要高亮变色即可,BottomNavigationView的这个属性labelVisibilityMode,有四个值,labeled unlabeled selected auto

其中selected,auto都会在点击导航按钮的时候item有一个弹起的动画会使各个导航按钮弹起导致中间被覆盖的按钮错位显示出来,所以我只能选择labeled和unlabeled,

bottom_nav_menu的item里面不写title属性会报错,我就写上了,

为了实现需求"只显示icon不需要显示title" 我先把labelVisibilityMode取 labeled, 在dimens.xml中把bottom_nav_menu的字体大小text_size和active_text_size都重写成0sp(太天真了)

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools">
    <dimen name="padding_horizontal">20dp</dimen>
    <dimen name="divider">1dp</dimen>
    
    <!--BottomNavigationView 是否选中状态下title字体大小-->
    <dimen name="design_bottom_navigation_active_text_size" tools:override="true">0sp</dimen>
    <dimen name="design_bottom_navigation_text_size" tools:override="true">0sp</dimen>

    <dimen name="design_bottom_navigation_margin">16dp</dimen>
</resources>

结果运行闪退就报开头的错误,找了好久没发现问题,active_text_size和text_size是不能取0的!!

方法1:所以我把dimens.xml文件里重写的active_text_size和text_size都删掉或者设置成非0sp,底部导航就连icon带title都显示出来了,这时要去掉title或者把title写成""就实现了需求

 

方法2:app:labelVisibilityMode="unlabeled"

后来我又试了一下labelVisibilityMode取剩下的属性unlabeled,直接实现了。。。

不管底部导航的active_text_size和text_size是不是重写成0sp、menu里的item的title值是不"”都可以实现了

 

 

 

本文地址:https://blog.csdn.net/qq_36252342/article/details/107382805

如您对本文有疑问或者有任何想说的,请 点击进行留言回复,万千网友为您解惑!

相关文章:

验证码:
移动技术网