【问题标题】:How to change specific item background in bottom navigation view programmatically?如何以编程方式更改底部导航视图中的特定项目背景?
【发布时间】:2020-03-07 11:56:18
【问题描述】:

我想更改底部导航视图中最后一个选项卡的背景颜色。我做了很多互联网搜索,但找不到任何解决方案。我将在这里附上我的所有代码。请在下面找到我想要实现的图像。

这是我的活动代码

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        val navView: BottomNavigationView = findViewById(R.id.nav_view)

        val navController = findNavController(R.id.nav_host_fragment)
        navView.setupWithNavController(navController)
    }
}

这是我的活动 xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/nav_view"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="0dp"
        android:layout_marginEnd="0dp"
        app:itemIconTint="@color/tab_selector"
        android:background="@drawable/shape_bottom_navigation"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:menu="@menu/bottom_nav_menu" />

    <fragment
        android:id="@+id/nav_host_fragment"
        android:name="androidx.navigation.fragment.NavHostFragment"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:defaultNavHost="true"
        app:layout_constraintBottom_toTopOf="@id/nav_view"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:navGraph="@navigation/mobile_navigation" />

</androidx.constraintlayout.widget.ConstraintLayout>

我的菜单 xml 文件

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

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_home"
        android:title="" />

    <item
        android:id="@+id/navigation_search"
        android:icon="@drawable/ic_search"
        android:title="" />

    <item
        android:id="@+id/navigation_page"
        android:icon="@drawable/ic_page"
        android:title="" />

    <item
        android:id="@+id/navigation_refresh"
        android:icon="@drawable/ic_refresh"
        android:title="" />

</menu>

【问题讨论】:

  • 你的菜单 xml 是什么?
  • 我在问题中添加了菜单 xml。请检查

标签: android android-activity android-menu bottomnavigationview android-jetpack-navigation


【解决方案1】:

使用以下代码更改了 BottomNavigationView 内特定选项卡的背景。

var bottomNavigationMenuView = (bottomNavigationView[0] as BottomNavigationMenuView)
if (bottomNavigationMenuView.isNotEmpty()) {
    bottomNavigationMenuView[TAB_INDEX].setBackgroundResource(R.drawable.shape_refresh)
}

在我的例子中,TAB_INDEX 是 3

【讨论】:

  • 如果这回答了您的问题,请将其标记为已接受。因此,从未回答的问题队列中删除您的问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-25
  • 1970-01-01
  • 2017-04-13
  • 1970-01-01
相关资源
最近更新 更多