当选择片段时,Android底部导航栏项目背景色如何改变?

8

我希望在底部导航栏按下时更改背景颜色(仅选定区域),就像下面的链接:

http://uupload.ir/files/nq3k_tab_bar.jpg

这是我的选择器xml:

    <?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto">
    <item android:state_checked="true" android:color="@color/beyez" />
    <item android:state_active="true" app:itemBackground="@color/backbeyez"/>
    <item android:color="@color/colorPrimaryDark"  />
</selector>

这是活动的 XML 代码:

<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottomnav"
    android:layout_width="382dp"
    android:layout_height="52dp"
    android:layout_marginBottom="0dp"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@drawable/nav_items_color"
    app:itemTextColor="@drawable/nav_items_color"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"

    app:menu="@menu/navigation"
    tools:layout_editor_absoluteY="515dp">

</android.support.design.widget.BottomNavigationView>

以下是我的Java类的代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    switchorders();

    BottomNavigationView bottomNavigationView = (BottomNavigationView)findViewById(R.id.bottomnav);
    BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);


    bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener()
    {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {

            switch (item.getItemId())
            {
                case R.id.navigation_orders:
                    switchorders();
                    break;

                case R.id.navigation_credit:
                    switchcredits();
                    break;

                case R.id.navigation_works:
                    switchworks();
                    break;

                case R.id.navigation_profile:
                    switchprofile();
                    break;

            }
            return true;
        }
    });

}

谢谢大家的帮助!
感谢你们的协助!

请编辑问题以指定您在实现此操作时遇到的确切问题。同时,请使用代码块添加您的代码,如果您愿意,可以保留链接,但我们更喜欢在这里使用代码块,因为链接可能会在未来失效。这会使未来遇到相同问题的人处于不利地位,因为他们无法查看代码。 - Lexi
网址只是带您进入网站,没有显示任何图像。 - SripadRaj
@SripadRaj 网址已编辑,感谢通知。 - user8426874
1个回答

19

只需在 xml 文件的 BottomNavigationView 中像您为其他参数(如 app:itemTextColor)所做的那样,将 app:itemBackground="@color/colorPrimary" 更改为可绘制对象即可。

编辑

这应该有效,我已经尝试了 SDK > 19。

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

app:itemBackground="@color/colorPrimary" 的参数不能设置为可绘制对象,应用程序会崩溃。 - user8426874
我更新了答案,我提供的那个应该可以用! - Samuele Pontremoli

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接