我正在使用导航视图来修改android应用。对于前几个菜单项,我给出了一个图标,但是此后,我仅给出了菜单标题。但是在导航面板中,没有图标的菜单项有一个空白区域。
如何删除空白空间?我希望菜单项开始时没有空白。
这是我的main_activity.xml代码:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include
layout="@layout/activity_category"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer">
<TextView
android:layout_width="match_parent"
android:layout_height="60dp"
android:text="Emoji icons provided free by EmojiOne(www.emojione.com)"
android:background="#424242"
android:textColor="@android:color/white"
android:textSize="12dp"
android:layout_gravity="bottom"
android:gravity="center"/>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
这是我的activity_main_drawer.xml代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:checkableBehavior="single">
<item
android:id="@+id/numbers_menu_item"
android:icon="@drawable/numbers"
android:title="Numbers" />
<item
android:id="@+id/family_menu_item"
android:icon="@drawable/family"
android:title="Family"/>
<item
android:id="@+id/colors_menu_item"
android:icon="@drawable/colors"
android:title="Colors"/>
<item
android:id="@+id/animals_menu_item"
android:icon="@drawable/animals"
android:title="Animals"/>
<item
android:id="@+id/food_menu_item"
android:icon="@drawable/coconut"
android:title="Food"/>
<item
android:id="@+id/pronoun_menu_item"
android:title="Pronouns" />
<item
android:id="@+id/pronoun_examples_menu_item"
android:title="Pronoun Examples"/>
<item
android:id="@+id/greetings_menu_item"
android:title="Greetings"/>
<item
android:id="@+id/introductions_menu_item"
android:title="Introductions"/>
<item
android:id="@+id/vendors_menu_item"
android:title="Talking with vendors"/>
<item
android:id="@+id/directions_menu_item"
android:title="Asking for directions"/>
<item
android:id="@+id/colleagues_menu_item"
android:title="Talking with colleagues"/>
<item
android:id="@+id/taxi_driver_menu_item"
android:title="Talking with taxi driver"/>
<item
android:id="@+id/questioning_words_menu_item"
android:title="Questioning Words"/>
</group>
<item android:title="Empty category"/>
</menu>
编辑:尝试将所有没有图标的菜单项分组到一个组中。还是没用。 代码:
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group
android:checkableBehavior="single">
<item
android:id="@+id/numbers_menu_item"
android:icon="@drawable/numbers"
android:title="Numbers" />
<item
android:id="@+id/family_menu_item"
android:icon="@drawable/family"
android:title="Family"/>
<item
android:id="@+id/colors_menu_item"
android:icon="@drawable/colors"
android:title="Colors"/>
<item
android:id="@+id/animals_menu_item"
android:icon="@drawable/animals"
android:title="Animals"/>
<item
android:id="@+id/food_menu_item"
android:icon="@drawable/coconut"
android:title="Food"/>
</group>
<group
android:checkableBehavior="single">
<item
android:id="@+id/pronoun_menu_item"
android:title="Pronouns" />
<item
android:id="@+id/pronoun_examples_menu_item"
android:title="Pronoun Examples"/>
<item
android:id="@+id/greetings_menu_item"
android:title="Greetings"/>
<item
android:id="@+id/introductions_menu_item"
android:title="Introductions"/>
<item
android:id="@+id/vendors_menu_item"
android:title="Talking with vendors"/>
<item
android:id="@+id/directions_menu_item"
android:title="Asking for directions"/>
<item
android:id="@+id/colleagues_menu_item"
android:title="Talking with colleagues"/>
<item
android:id="@+id/taxi_driver_menu_item"
android:title="Talking with taxi driver"/>
<item
android:id="@+id/phone_menu_item"
android:title="Talking in phone"/>
<item
android:id="@+id/questioning_words_menu_item"
android:title="Questioning Words"/>
<item android:title="Empty category"/>
</group>
</menu>
答案 0 :(得分:3)
从NavigationMenuItemView和NavigationMenuPresenter源代码中,如果该组具有图标或任何菜单项具有图标,它将添加空图标。因此,按照建议,您需要将所有文本菜单分组到一个没有图标的单独的组中。您必须为每个组指定一个不同的组ID。
private void prepareMenuItems() {
if (mUpdateSuspended) {
return;
}
mUpdateSuspended = true;
mItems.clear();
mItems.add(new NavigationMenuHeaderItem());
int currentGroupId = -1;
int currentGroupStart = 0;
boolean currentGroupHasIcon = false;
for (int i = 0, totalSize = mMenu.getVisibleItems().size(); i < totalSize; i++) {
MenuItemImpl item = mMenu.getVisibleItems().get(i);
if (item.isChecked()) {
setCheckedItem(item);
}
if (item.isCheckable()) {
item.setExclusiveCheckable(false);
}
if (item.hasSubMenu()) {
SubMenu subMenu = item.getSubMenu();
if (subMenu.hasVisibleItems()) {
if (i != 0) {
mItems.add(new NavigationMenuSeparatorItem(mPaddingSeparator, 0));
}
mItems.add(new NavigationMenuTextItem(item));
boolean subMenuHasIcon = false;
int subMenuStart = mItems.size();
for (int j = 0, size = subMenu.size(); j < size; j++) {
MenuItemImpl subMenuItem = (MenuItemImpl) subMenu.getItem(j);
if (subMenuItem.isVisible()) {
if (!subMenuHasIcon && subMenuItem.getIcon() != null) {
subMenuHasIcon = true;
}
if (subMenuItem.isCheckable()) {
subMenuItem.setExclusiveCheckable(false);
}
if (item.isChecked()) {
setCheckedItem(item);
}
mItems.add(new NavigationMenuTextItem(subMenuItem));
}
}
if (subMenuHasIcon) {
appendTransparentIconIfMissing(subMenuStart, mItems.size());
}
}
} else {
int groupId = item.getGroupId();
if (groupId != currentGroupId) { // first item in group
currentGroupStart = mItems.size();
currentGroupHasIcon = item.getIcon() != null;
if (i != 0) {
currentGroupStart++;
mItems.add(new NavigationMenuSeparatorItem(
mPaddingSeparator, mPaddingSeparator));
}
} else if (!currentGroupHasIcon && item.getIcon() != null) {
currentGroupHasIcon = true;
appendTransparentIconIfMissing(currentGroupStart, mItems.size());
}
NavigationMenuTextItem textItem = new NavigationMenuTextItem(item);
textItem.needsEmptyIcon = currentGroupHasIcon;
mItems.add(textItem);
currentGroupId = groupId;
}
}
mUpdateSuspended = false;
}