在导航视图中,当我选择项目时,所选项目应以粗体显示并增加字体大小。我如何实现这一目标?
基本上,我想为导航项添加一个选择器,它将在选定状态下显示粗体字体,在非选择状态下显示普通字体。
由于
答案 0 :(得分:2)
Styles.xml
<style name="NavDrawerTextStyle" parent="Base.TextAppearance.AppCompat">
<item name="android:textColor">@color/colorPrimaryDark</item>
<item name="android:textSize">30sp</item>
<item name="android:textStyle">bold</item>
</style>
然后在您的NavigationView中:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.NavigationView
...
app:itemTextAppearance="@style/NavDrawerTextStyle"
/>
</android.support.v4.widget.DrawerLayout>
答案 1 :(得分:2)
尝试以下
//put this in main Activity
private void setFontToMenuItem(MenuItem mi) {
Typeface font = Typeface.createFromAsset(getAssets(), "fonts/customfont.ttf");
SpannableString mNewTitle = new SpannableString(mi.getTitle());
mNewTitle.setSpan(new CustomTypefaceSpan("" , font), 0 , mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
mi.setTitle(mNewTitle);
}
像这样使用
navigationView = (NavigationView) findViewById(R.id.navigation_view);
Menu m = navigationView.getMenu();
for (int i=0;i<m.size();i++) {
MenuItem item = m.getItem(i);
//for aapplying a font to subMenu ...
SubMenu subMenu = item.getSubMenu();
if (subMenu!=null && subMenu.size() >0 ) {
for (int j=0; j <subMenu.size();j++) {
MenuItem subMenuItem = subMenu.getItem(j);
setFontToMenuItem(subMenuItem);
}
}
//the method we have create in activity
setFontToMenuItem(item);
}