我使用BottomNavigationView
。始终会检查第一个菜单项,但是在我的应用中,启动时无需检查任何菜单项。我该怎么办?
这是我的代码:
my_menu.xml
文件:
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/help"
android:title="@string/help"
android:checked="false"
android:enabled="true"
android:icon="@drawable/ic_question"
app:showAsAction="always|withText"/>
<item android:id="@+id/places"
android:title="@string/places"
android:checked="false"
android:enabled="true"
android:icon="@drawable/ic_places"
app:showAsAction="always|withText"/>
<item android:id="@+id/search"
android:title="@string/search"
android:checked="false"
android:enabled="true"
android:icon="@drawable/ic_search"
app:showAsAction="always|withText"/>
<item android:id="@+id/add"
android:title="@string/add"
android:checked="false"
android:enabled="true"
android:icon="@drawable/ic_add"
app:showAsAction="always|withText"/>
activity_main_layout.xml
文件:
<android.support.design.widget.BottomNavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bnvBottomMenu"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:layout_gravity="bottom"
app:menu="@menu/my_menu" />
MainActivity.class
文件:
bnvBottomMenu.setOnNavigationItemSelectedListener(new
BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.help:
break;
case R.id.places:
break;
case R.id.search:
break;
case R.id.add:
break;
}
return false;
}
});
编辑:checked =“ false”参数无效
答案 0 :(得分:0)
使用此代码:
my_menu.xml
<?xml version="1.0" encoding="utf-8"?><menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/help"
android:title="@string/help"
android:checked="false" //you set this attribute true
android:enabled="true"
android:icon="@drawable/ic_question"
app:showAsAction="always|withText"/>
<item android:id="@+id/places"
android:title="@string/places"
android:checked="false"
android:enabled="true"
android:icon="@drawable/ic_places"
app:showAsAction="always|withText"/>
<item android:id="@+id/search"
android:title="@string/search"
android:checked="false"
android:enabled="true"
android:icon="@drawable/ic_search"
app:showAsAction="always|withText"/>
<item android:id="@+id/add"
android:title="@string/add"
android:checked="false"
android:enabled="true"
android:icon="@drawable/ic_add"
app:showAsAction="always|withText"/>