问题
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?
回答1:
They're not supposed to be the same. From the Navigation Drawer guidelines, you'll note this screenshot:
The side margin is 16dp
(and 24dp
on tablets) while the titles are aligned with the 72dp
keyline.
Since the NavigationView
seeks to exactly match the material design guidelines, there is no way to change the spacing.
来源:https://stackoverflow.com/questions/37058219/how-to-programmatically-set-the-margin-between-icon-and-title-of-menu-item-of-na