问题描述
我正在开发一个Android应用程序.在我的应用程序中,我正在使用导航抽屉和导航视图.但是我以编程方式为他们设置了菜单项.对于菜单项,我同时设置了图标和标题.但是我对此有疑问.也就是说,我无法设置以编程方式添加到菜单的图标和标题之间的间距.
I am developing an Android App. In my app, I am using navigation drawer and navigation view. But I set the menu item for them programmatically. For a menu item, I set both icon and title. But I am having a problem with that. That is I cannot set the spacing between icon and title that I programmatically added to menu.
这是我以编程方式将项目添加到菜单的方式
This is how I programmatically add item to menu
Menu menu = leftDrawer.getMenu();
SubMenu subMenu = menu.addSubMenu(MAIN_MENU_ITEM_GROUP_ID, 99, 99, "Others");
subMenu.add(MAIN_MENU_ITEM_GROUP_ID,96,96,"Monthly Leaderboard").setIcon(R.drawable.leaderboard_icon).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
startActivity(new Intent(MainActivity.this, LeaderboardActivity.class));
return false;
}
});
subMenu.add(MAIN_MENU_ITEM_GROUP_ID,96,96,"Settings").setIcon(R.drawable.settings_icon).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
return false;
}
});
但是图标和标题之间的间距设置不正确.
But spacing between between icon and title is not properly set.
这是我的问题的屏幕截图
This is the screenshot of my problem
如屏幕截图所示,屏幕左侧和图标之间的间距以及图标和标题之间的间距不相同.如何通过编程设置图标和菜单项标题之间的间距?
As you can see in screenshot, spacing between left side of screen and icon and spacing between icon and title are not same. How can I set the spacing between icon and title of menu item programmatically?
推荐答案
它们不应该是相同的.在导航抽屉指南中,您会注意到此屏幕截图:
They're not supposed to be the same. From the Navigation Drawer guidelines, you'll note this screenshot:
当标题与72dp
键线对齐时,边距为16dp
(在平板电脑上为24dp
).
The side margin is 16dp
(and 24dp
on tablets) while the titles are aligned with the 72dp
keyline.
由于NavigationView
试图与材料设计准则完全匹配,所以无法更改间距.
Since the NavigationView
seeks to exactly match the material design guidelines, there is no way to change the spacing.
这篇关于如何在Android中以编程方式设置图标和导航抽屉菜单项标题之间的边距?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!