如何以编程方式更改底部导航视图中的特定项目背景?
How to change specific item background in bottom navigation view programmatically?
我想更改底部导航视图中最后一个选项卡的背景颜色。我做了很多互联网搜索,但找不到任何解决方案。我要在这里附上我的所有代码。请找到我想要实现的下图。
这是我的 activity 代码
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)
}
}
这是我的activityxml
<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>
使用以下代码更改了 BottomNavigationView 中特定选项卡的背景。
var bottomNavigationMenuView = (bottomNavigationView[0] as BottomNavigationMenuView)
if (bottomNavigationMenuView.isNotEmpty()) {
bottomNavigationMenuView[TAB_INDEX].setBackgroundResource(R.drawable.shape_refresh)
}
在我的例子中 TAB_INDEX 是 3
我想更改底部导航视图中最后一个选项卡的背景颜色。我做了很多互联网搜索,但找不到任何解决方案。我要在这里附上我的所有代码。请找到我想要实现的下图。
这是我的 activity 代码
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)
}
}
这是我的activityxml
<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>
使用以下代码更改了 BottomNavigationView 中特定选项卡的背景。
var bottomNavigationMenuView = (bottomNavigationView[0] as BottomNavigationMenuView)
if (bottomNavigationMenuView.isNotEmpty()) {
bottomNavigationMenuView[TAB_INDEX].setBackgroundResource(R.drawable.shape_refresh)
}
在我的例子中 TAB_INDEX 是 3