使用底部导航栏选项卡的不同背景色

时间:2018-09-11 14:28:00

标签: java android bottomnavigationview

我正在向项目添加BottomNavigationView,并且希望每个底部标签具有不同的背景颜色。在颜色选择器资源文件中使用android:state_selected="true"使用其他颜色似乎无效我也尝试输入更多类似android:state_focused =“ true”或android:state_enabled =“ true”之类的东西,不幸的是没有任何效果。

(BottomnavigationView)

 <android.support.design.widget.BottomNavigationView
        android:id="@+id/bottomNavigationView"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:itemIconTint="@color/menu_txt_color"
        app:itemTextColor="@color/menu_txt_color"
        android:background="@drawable/bnv_tab_item_foreground"
        app:layout_constraintBottom_toTopOf="@+id/guideline14"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:menu="@menu/navigation" />

(颜色选择器)

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@color/add_child" android:state_checked="true" android:state_pressed="true"/>
    <item android:drawable="@color/light_blue" android:state_checked="false"/>

</selector>

(我的菜单资源)

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <item
        android:id="@+id/tab1"
        android:layout_height="wrap_content"
        android:icon="@drawable/baby3"
        android:checked="true"
        android:title="hello"
        app:showAsAction="always|withText" />

    <item
        android:id="@+id/tab2"
        android:icon="@drawable/baby2"
        android:title="heelo2"
        app:showAsAction="always|withText|collapseActionView" />


    <item
        android:id="@+id/tab3"
        android:checkable="false"
        android:icon="@drawable/baby2"
        android:title="hello 3"
        app:showAsAction="always|withText" />

    <item
        android:id="@+id/tab4"
        android:checkable="false"
        android:icon="@drawable/baby3"
        android:title="heelo 4"
        app:showAsAction="always|withText" />

</menu>

选择后突出显示选项卡,我想要Instagram之类的BottomNavigationBar选项卡,我也尝试过(live),但没有帮助。.

有人遇到过这种情况吗?

谢谢

1 个答案:

答案 0 :(得分:1)

尝试这种方式

使用“自定义操作”布局作为菜单

$Array = @()
$Computers = "Computer1","Computer2","Computer3"

Foreach ($Computer in $Computers)
{
$Services = Get-WmiObject Win32_Service -ComputerName $Computer | Select name,state,startmode

    Foreach ($Service in $Services)
    {
        $Array += $Service
    }

}

$Array | Format-Table

<item android:id="@+id/nav_1" android:title="" app:actionLayout="@layout/custom_layout"/> <item android:id="@+id/nav_2" android:title="" app:actionLayout="@layout/custom_layout"/> <item android:id="@+id/nav_3" android:title="" app:actionLayout="@layout/custom_layout"/> <item android:id="@+id/nav_4" android:title="" app:actionLayout="@layout/custom_layout"/> <item android:id="@+id/nav_5" android:title="" app:actionLayout="@layout/custom_layout"/> custom_layout文件夹中将是这样

layout

在使用<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <Button android:id="@+id/button" android:layout_width="match_parent" android:layout_height="0dp" android:layout_gravity="center_vertical" android:background="@drawable/selector" android:layout_weight="1" android:gravity="center_vertical"/> </LinearLayout> 的“活动”中,您可以这样做

BottomNavigationView

更新

selector.xml

try{
    View view = navigationView.getMenu().findItem(R.id.nav_1).getActionView();
            Button button = (Button) view.findViewById(R.id.button);
             //button.setOnClickListener(this);
             // similarly for others menu in BottomNavigationView
        } catch (Exception e) {
            AppLogger.getInstance().writeException(e);
        }