custom menu item size and textsize in navigation view

半世苍凉 提交于 2019-12-01 17:31:10

问题


I am using the code to customize height of text and row height in navigation view and it works on api21+ but the same code is not working in api 16. How can I make it work ?

COde:

Create a style and apply it to NavigationView using app:theme

<style name="NavigationViewStyle">
     <item name="android:textSize">20sp</item> <!-- menu item text size-->
     <item name="android:listPreferredItemHeightSmall">40dp</item><!-- menu item height-->
</style>

And then, apply this style to NavigationView using app:theme

<android.support.design.widget.NavigationView
       ...
       ...    
        app:theme="@style/NavigationViewStyle"
       ...
       ...


</android.support.design.widget.NavigationView>

回答1:


In code you can use this code to customize text view. try this and let me know.

private final ArrayList<View> mMenuItems = new ArrayList<>();
    final Menu navMenu = navigationView.getMenu();
    // Install an OnGlobalLayoutListener and wait for the NavigationMenu to fully initialize
    navigationView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            // Remember to remove the installed OnGlobalLayoutListener
            navigationView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            // Loop through and find each MenuItem View
            for (int i = 0; i < navMenu.size(); i++) {
                final String id = "nav_menuItem" + (i);
                final MenuItem item = navMenu.findItem(getResources().getIdentifier(id, "id", getPackageName()));
                navigationView.findViewsWithText(mMenuItems, item.getTitle(), View.FIND_VIEWS_WITH_TEXT);
            }
            // Loop through each MenuItem View and apply your custom Typeface

            for (final View menuItem : mMenuItems) {
                ((TextView) menuItem).setTypeface(fontLight, Typeface.NORMAL);
            }

        }
    });



回答2:


Check your res/values directories, you may find a res/values-v16 directory that implements a different style



来源:https://stackoverflow.com/questions/35313302/custom-menu-item-size-and-textsize-in-navigation-view

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