【问题标题】:Animation BottomNavigationView doesn't work动画底部导航视图不起作用
【发布时间】:2020-01-12 00:25:45
【问题描述】:

当我点击 BottomNavigationView 项目时,它在某些版本(例如 Marshallow)上没有动画

预期行为是

当我点击项目时,图标变大并出现文本。

但结果是

图标保持正常大小并显示文本

代码

build.gradle(应用程序)

//AndroidX UI
implementation "com.google.android.material:material:1.0.0"
implementation "androidx.appcompat:appcompat:1.1.0"

activity.xml

<com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/activity_home_custombottombar"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintVertical_bias="1"
            android:layout_marginStart="0dp"
            android:layout_marginEnd="0dp"
            android:background="@color/app_black"
            app:itemIconTint="@drawable/nav_bottombar_item"
            app:itemTextColor="@drawable/nav_bottombar_item"
            app:labelVisibilityMode="selected"
            app:menu="@menu/menu_items" />

menu_item.xml

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


    <item
            android:id="@+id/action_1"
            android:icon="@drawable/ic_home_black_24dp"
            android:title="@string/menu_action_1" />

    <item
            android:id="@+id/action_2"
            android:icon="@drawable/ic_dashboard_black_24dp"
            android:title="@string/menu_action_2" />

    <item
            android:id="@+id/action_3"
            android:icon="@drawable/ic_notifications_black_24dp"
            android:title="@string/menu_action_3" />

</menu>

nav_bottombar_item.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/app_white" android:state_enabled="true" />
    <item android:color="@color/app_black" android:state_enabled="false" />
</selector>

HomeActivity.kt

bottomNavigationView.setOnNavigationItemSelectedListener {
            when(it.itemId) {
                R.id.action_1 -> {
                    viewModel.navigationHandler?.switchStack(PredefinedStacks.STACK_ACTION_1)
                    true
                }
                R.id.action_2 -> {
                    viewModel.navigationHandler?.switchStack(PredefinedStacks.STACK_ACTION_2)
                    true
                }
                R.id.action_3 -> {
                    viewModel.navigationHandler?.switchStack(PredefinedStacks.STACK_ACTION_3)
                    true
                }
                else -> false
            }
        }


我期望和结果:

如果您可以在下一张图片中看到动画无法正常工作。
我期待这样的事情:


但是我得到了这个结果:

更新:

我只需要在 setOnNavigationItemSelectedListener 上调用 requestLayout。 显然,BottomNavigationView 仍然被阻塞,它只需要重绘。

bottomNavigationView.setOnNavigationItemSelectedListener {
bottomNavigationView.requestLayout()
            when(it.itemId) {
                R.id.action_1 -> {
                    viewModel.navigationHandler?.switchStack(PredefinedStacks.STACK_ACTION_1)
                    true
                }
                R.id.action_2 -> {
                    viewModel.navigationHandler?.switchStack(PredefinedStacks.STACK_ACTION_2)
                    true
                }
                R.id.action_3 -> {
                    viewModel.navigationHandler?.switchStack(PredefinedStacks.STACK_ACTION_3)
                    true
                }
                else -> false
            }
        }

【问题讨论】:

  • 请尝试app:labelVisibilityMode="labeled"
  • 如果我添加 labelVisibilityMode="labeled" 标签一直显示,我不希望这样。
  • android:background="@color/app_black" 如果不让整个事情变黑,你认为这条线会做什么
  • 如果我删除背景,我想要黑色背景,行为是相同的。
    预期行为:
    当我点击项目时,图标变大并出现文本。错误结果:
    图标保持正常大小并显示文本。

标签: android animation kotlin bottomnavigationview material-components-android


【解决方案1】:

对您的nav_bottombar_item.xmlapp:itemTextColor="@drawable/nav_bottombar_item" 进行这些更改:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/app_white" android:state_checked="true" />
    <item android:color="@color/app_black" android:state_checked="false" />
</selector>

并添加此app:labelVisibilityMode="labeled"

【讨论】:

  • 使用此代码我获得:i.ibb.co/71JHBcg/result.png 并且预期的行为是:当我单击项目时,图标变大并出现文本。但我得到错误结果:
    图标保持正常大小并出现文本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-13
  • 2021-10-20
  • 2023-03-17
  • 2021-08-18
  • 1970-01-01
  • 1970-01-01
  • 2017-03-02
相关资源
最近更新 更多