将字体大小添加到 android 中的 NavigationView 项目
Adding font size to NavigationView item in android
在 navigationView 中,当我 select 一个项目时,selected 项目应该以粗体显示并增加字体大小。我该如何实现?
基本上,我想为导航项添加一个 selector,它将在 selected 状态下显示粗体字体,在非 selected 状态下显示普通字体。
谢谢
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>
像下面这样尝试
//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);
}
在 navigationView 中,当我 select 一个项目时,selected 项目应该以粗体显示并增加字体大小。我该如何实现?
基本上,我想为导航项添加一个 selector,它将在 selected 状态下显示粗体字体,在非 selected 状态下显示普通字体。
谢谢
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>
像下面这样尝试
//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);
}