How to programmatically set the margin between icon and title of menu item of Navigation Drawer in Android?

时光毁灭记忆、已成空白 提交于 2020-01-03 05:42:06

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!