【发布时间】:2017-05-16 05:58:48
【问题描述】:
我已经实现了BottomNavigationView,它可以从新的支持库 25.0.0 中获得。这是我的代码
<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="@drawable/text"
app:itemTextColor="@drawable/text"
app:menu="@menu/bottom_navigation_main" />
还有text.xmldrawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="@android:color/white" android:state_enabled="true" />
<item android:color="@color/colorPrimaryDark" android:state_enabled="false" />
</selector>
使用此代码,我可以在单击菜单项时更改文本颜色,但是当我将相同的内容应用于 app:itemBackground 时,它会显示错误 <item> tag requires a 'drawable' attribute or child tag defining a drawable。
这是我尝试过的app:itemBackground
app:itemBackground="@drawable/text"
所以我的问题是如何更改所选菜单项的背景颜色?
【问题讨论】:
-
创建一个可绘制文件并设置为
app:itemBackground。根据BottomNavvigationView Doc可以只设置资源为背景。 -
尝试在你的项目中添加 android:drawable="@drawable/item_background_selected"
-
@AnkitaShah 是的,这就是我所做的,我已经尝试过
app:itemBackground="@color/colorPrimary",这只是可绘制文件 -
@Charuka 完成了,我们不能将 android:color 用于
itemBackground,我们必须在“- ”中使用
android:drawable,您的答案部分正确。但是我们也不能不包括<item>和android:color,这会产生错误。 - ”中使用
-
@Ravi Rupareliya 很酷,我在用我的手机,否则我会拍得更好:P :))
标签: android bottomnavigationview