我在导航视图中使用代码来自定义文本高度和行高,它适用于api21,但是相同的代码在api 16中不起作用.我怎样才能使它工作?
码:
创建一个样式并使用app:theme将其应用于NavigationView
<style name="NavigationViewStyle"> <item name="android:textSize">20sp</item> <!-- menu item text size--> <item name="android:listPreferredItemHeightSmall">40dp</item><!-- menu item height--> </style>
然后,使用app:theme将此样式应用于NavigationView
<android.support.design.widget.NavigationView ... ... app:theme="@style/NavigationViewStyle" ... ... </android.support.design.widget.NavigationView>
解决方法
在代码中,您可以使用此代码自定义文本视图.试试看,让我知道.
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); } } });