如何在底部导航视图中更改所选项目的背景颜色

时间:2019-08-02 08:12:02

标签: android xml bottomnavigationview

我想在导航视图中更改所选项目的背景颜色。

我尝试使用选择器颜色文件并且也可以绘制,但是没有任何效果。 当我在itemBackground属性上运行所有时间异常时。.

background_color_tab.xml

package main

import (
    "fmt"
)

// a simple type, with two methods : one with a value receiver, one with a pointer receiver
type Item struct {
    name string
}

func (i Item) GetNameByValue() string {
    return i.name
}

func (i *Item) GetNameByRef() string {
    return i.name
}

func main() {
    {
        // in this map, we store *pointers* to Item values
        mapByRef := make(map[int]*Item)

        mapByRef[0] = &Item{"I am stored as a pointer"}

        // GetNameByRef will work on a *Item : "mapByRef[0]" is already a pointer
        fmt.Println("GetByRef   :", mapByRef[0].GetNameByRef())

        // GetNameByValue will work on a *Item :   go automatically turns this into '(*mapByRef[0]).GetNameByValue()', and this is valid
        fmt.Println("GetByValue :", mapByRef[0].GetNameByValue())
    }

    {
        // in this map, we store Item values (no pointers)
        mapByValue := make(map[int]Item)

        mapByValue[0] = Item{"I am stored as a value"}

        // GetNameByValue will work on a Item :  "mapByValue[0]" has the right type
        fmt.Println("GetByValue :", mapByValue[0].GetNameByValue())

        // GetNameByRef will not work :  go tries to turn this into :  (&mapByValue[0]).GetNameByRef(),
        // and go refuses to let you take the address of a value inside a map

        // fmt.Println("GetByRef   :", mapByValue[0].GetNameByRef())

        // compiler error :
        //   ./prog.go:47:46: cannot call pointer method on mapByValue[0]
        //   ./prog.go:47:46: cannot take the address of mapByValue[0]

        // you will need some way to copy the value before taking its address :
        item := mapByValue[0]
        fmt.Println("item.GetByRef    :", item.GetNameByRef())
        // same as :
        fmt.Println("(&item).GetByRef :", (&item).GetNameByRef())
    }
}

// Output :
//
// GetByRef   : I am stored as a pointer
// GetByValue : I am stored as a pointer
// GetByValue : I am stored as a value
// item.GetByRef    : I am stored as a value
// (&item).GetByRef : I am stored as a value

底部导航 第49行是(app:itemBackground)

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

错误

  

java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.ccms / com.example.ccms.MainActivity}:   android.view.InflateException:二进制XML文件第49行:二进制XML   文件行#49:错误放大类   android.support.design.widget.BottomNavigationView

1 个答案:

答案 0 :(得分:0)

您不能在app:itemBackground中设置颜色资源,必须在此属性中设置可绘制资源

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