我在代码部分所做的方式。动态生成的菜单代码。
for(int i = 0;i<menuDetails.size();i++){
int res_id = this.getResources().getIdentifier("@drawable/"+menuDetails.get(i).getMenu_icon(), null, this.getPackageName());
navigation.getMenu()
.add(Menu.NONE, i, Menu.NONE, menuDetails.get(i).getTitle())
.setIcon(res_id).setChecked(false)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);
}
XML 文件:
<FrameLayout
android:id="@+id/home_footer"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_nav_height"
android:layout_alignParentBottom="true">
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="@dimen/bottom_nav_height"
android:theme="@style/Widget.BottomNavigationView"
app:elevation="5dp"
app:itemIconTint="@drawable/nav_selection"
app:itemTextColor="@color/welcome_text_color"
app:labelVisibilityMode="labeled"
app:layout_constraintBottom_toBottomOf="parent" />
<!-- app:itemIconTint="@drawable/nav_selection" -->
</FrameLayout>
nav_selection 可绘制文件:
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="@drawable/ic_call"/>
<item android:state_checked="false" android:drawable="@drawable/nav_case" />