gpt4 book ai didi

android导航 View 菜单项之前(左侧)的空白空间

转载 作者:行者123 更新时间:2023-11-29 16:34:40 30 4
gpt4 key购买 nike

我正在使用导航 View 修改 Android 应用程序。对于前几个菜单项,我给出了一个图标,但之后,我只给出了菜单标题。但是在导航面板中,没有图标的菜单项有一个空白区域。

enter image description here

如何删除空白区域?我希望菜单项在没有空格的情况下开始。

这是我的 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>

最佳答案

从 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;
}

关于android导航 View 菜单项之前(左侧)的空白空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52756423/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com