gpt4 book ai didi

Android:底部导航 View - 更改所选项目的图标

转载 作者:IT老高 更新时间:2023-10-28 21:41:42 25 4
gpt4 key购买 nike

我在我的应用程序中添加了 BottomNavigationView

ma​​in.xml

<android.support.design.widget.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="@color/colorPrimary"
app:itemIconTint="@color/white"
app:itemTextColor="@color/white"
app:menu="@menu/bottom_navigation_main" />

bottom_navigation_main.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_favorites"
android:enabled="true"
android:icon="@drawable/ic_favorite_white_24dp"
android:title="@string/text_favorites"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_schedules"
android:enabled="true"
android:icon="@drawable/ic_access_time_white_24dp"
android:title="@string/text_schedules"
app:showAsAction="ifRoom" />
<item
android:id="@+id/action_music"
android:enabled="true"
android:icon="@drawable/ic_audiotrack_white_24dp"
android:title="@string/text_music"
app:showAsAction="ifRoom" />
</menu>

MainActivity 点击

bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_favorites:
//need change icon of favotites here.
case R.id.action_schedules:

case R.id.action_music:

}
return true;
}
});

我想更改所选位置底部导航的图标。当用户单击一个项目时,我们如何实现此功能?

(如果用户点击了一项,则图标将变为另一项)

最佳答案

您可以简单地在drawable文件夹中创建drawable选择器,并且可以根据 View 中使用的小部件的状态更改图像

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/calender_green" android:state_checked="true"/>
<item android:drawable="@drawable/calender_black" android:state_checked="false"/>
</selector>

关于Android:底部导航 View - 更改所选项目的图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41826122/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com