How to change the indentation of sub menu items in a NavigationView?

半城伤御伤魂 提交于 2019-12-10 12:57:16

问题


When we define a NavigationView with a section with sub menu items. It left aligns the sub items with the section title:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:title="Sub items">
    <menu>
        <item
            android:title="Sub item 1" />
        <item
            android:title="Sub item 2" />
    </menu>
</item>
</menu>

I tried adding a transparent image with the correct size to pad:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

<item android:title="Sub items">
    <menu>
        <item
            android:icon="@drawable/ic_transparent"
            android:title="Sub item 1" />
        <item
            android:icon="@drawable/ic_transparent"
            android:title="Sub item 2" />
    </menu>
</item>
</menu>

But by default the NavigationView:

  1. Adds a fixed padding between the icon the text
  2. Enforces a fixes size on the icon itself

I could not find how to configure this padding nor the icon size.

Question How can we change the sub item indentation so that the sub items are more indented?

I prefer to do it cleaning via an attribute rather than inserting transparent images.


回答1:


Disclaimer

See my comment to Noundla on the main question where I explain why I think that indentation is not the right approach.

Answer

With that said, if you must have indentation the simplest way is to pad each menu item with spaces. This is not ideal but it is simple to implement, understand and replace when a better option is available. And it works with the built-in Android NavigationView without having to introduce external libraries:

Here is the code sample that will work:

<menu xmlns:android="http://schemas.android.com/apk/res/android">

  <item android:title="Sub items">
    <menu>
        <item android:title="&#160;&#160;&#160;&#160;Sub item 1" />
        <item android:title="&#160;&#160;&#160;&#160;Sub item 2" />
    </menu>
   </item>
</menu>

I hope this saves someone out there time.




回答2:


Not sure if you got the answer but I had the same problem and ended up using MaterialDrawer - https://github.com/mikepenz/MaterialDrawer.

You need to extend the SecondaryDrawerItem and add the padding on bindView onPostBindView.

drawer = new DrawerBuilder()
            .withActivity(this)
            .withHeader(drawerHeader)
            .withSavedInstance(savedInstanceState)
            .addDrawerItems(
                    new PrimaryDrawerItem().withName("Item1"),
                    new CustomSecondaryDrawerItem().withName("SubItem1"),
                    new CustomSecondaryDrawerItem().withName("SubItem2")
            )
            .build();

CustomDrawerSecondaryItem.java

public class CustomSecondaryDrawerItem extends SecondaryDrawerItem {

   @Override
   public void onPostBindView(IDrawerItem drawerItem, View view) {

       Context ctx = view.getContext();

       int paddingLeft = ctx.getResources().getDimensionPixelSize(R.dimen.drawer_secondary_item_padding_left);
       view.setPadding(paddingLeft, view.getPaddingTop(), view.getPaddingRight(), view.getPaddingBottom());

       super.onPostBindView(drawerItem, view);

   }
}


来源:https://stackoverflow.com/questions/31528998/how-to-change-the-indentation-of-sub-menu-items-in-a-navigationview

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