问题描述
我想在android.I中使用自己的字体作为导航抽屉。根据这个答案,我使用android studio自带的库:。但我不知道如何改变字体,并使其RTL。
我搜索了很多,我发现如何使抽屉RTL。我使用这个代码:
$ $ p $ getWindow()。getDecorView()。setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
和
但是,如你所知,这只适用于API 17及以上版本。
请帮忙!如何更改菜单字体?
编辑:
我的字体TTF文件是资产/字体我知道如何设置它为一个textview使用java,但我不知道如何将其设置为导航抽屉菜单。
我找到了答案:
首先在你的项目中创建这个类:
$ b $ $ p $ import android.graphics.Paint ;
导入android.graphics.Typeface;
导入android.text.TextPaint;
导入android.text.style.TypefaceSpan;
公共类CustomTypefaceSpan扩展TypefaceSpan {
private final Typeface newType;
public CustomTypefaceSpan(String family,Typeface type){
super(family);
newType = type;
$ b @Override
public void updateDrawState(TextPaint ds){
applyCustomTypeFace(ds,newType);
}
@Override
public void updateMeasureState(TextPaint paint){
applyCustomTypeFace(paint,newType);
private static void applyCustomTypeFace(Paint paint,Typeface tf){
int oldStyle;
Typeface old = paint.getTypeface();
if(old == null){
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int fake = oldStyle& 〜tf.getStyle(); ((fake& Typeface.BOLD)!= 0){
paint.setFakeBoldText(true); ((fake& Typeface.ITALIC)!= 0){
paint.setTextSkewX(-0.25f);
}
if
}
paint.setTypeface(tf);
$ b然后将此方法添加到要更改的活动导航抽屉菜单的字体:
pre prerivate $ void applyFontToMenuItem(MenuItem mi){
Typeface font = Typeface.createFromAsset getAssets(),ds_digi_b.TTF);
SpannableString mNewTitle = new SpannableString(mi.getTitle());
mNewTitle.setSpan(new CustomTypefaceSpan(,font),0,mNewTitle.length(),Spannable.SPAN_INCLUSIVE_INCLUSIVE);
mi.setTitle(mNewTitle);
}
,然后添加您刚添加到您的活动中的方法:
navView =(NavigationView)findViewById(R.id.navView);
Menu m = navView.getMenu();
for(int i = 0; i< m.size(); i ++){
MenuItem mi = m.getItem(i);
//用于将字体应用于子菜单...
SubMenu subMenu = mi.getSubMenu();
if(subMenu!= null& subMenu.size()> 0){
for(int j = 0; j< subMenu.size(); j ++){
MenuItem subMenuItem = subMenu.getItem(j);
applyFontToMenuItem(subMenuItem);
//我们在活动
中创建的方法applyFontToMenuItem(mi);
}
I want to use my own font for navigation drawer in android.I use the library comes with android studio according to this answer: https://stackoverflow.com/a/23632492/4393226.But I don't know how to change the font and make it RTL.I searched a lot and I found how to make the drawer RTL. I use this code:
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
andAndroid - Is Navigation Drawer from right hand side possible?
But as you know this only works in API 17 and above.Please help! How to change the menu font? How to make the layout RTL in right way?!
Edited:My font "TTF" file is in assets/fonts and I know how to set it for a textview using java, but I don't know how to set it to navigation drawer menu.
I found the answer:First create this class in your project:
import android.graphics.Paint;
import android.graphics.Typeface;
import android.text.TextPaint;
import android.text.style.TypefaceSpan;
public class CustomTypefaceSpan extends TypefaceSpan {
private final Typeface newType;
public CustomTypefaceSpan(String family, Typeface type) {
super(family);
newType = type;
}
@Override
public void updateDrawState(TextPaint ds) {
applyCustomTypeFace(ds, newType);
}
@Override
public void updateMeasureState(TextPaint paint) {
applyCustomTypeFace(paint, newType);
}
private static void applyCustomTypeFace(Paint paint, Typeface tf) {
int oldStyle;
Typeface old = paint.getTypeface();
if (old == null) {
oldStyle = 0;
} else {
oldStyle = old.getStyle();
}
int fake = oldStyle & ~tf.getStyle();
if ((fake & Typeface.BOLD) != 0) {
paint.setFakeBoldText(true);
}
if ((fake & Typeface.ITALIC) != 0) {
paint.setTextSkewX(-0.25f);
}
paint.setTypeface(tf);
}
}
Then add this method to your activity you want to change the font of navigation drawer menu:
private void applyFontToMenuItem(MenuItem mi) {
Typeface font = Typeface.createFromAsset(getAssets(), "ds_digi_b.TTF");
SpannableString mNewTitle = new SpannableString(mi.getTitle());
mNewTitle.setSpan(new CustomTypefaceSpan("" , font), 0 , mNewTitle.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
mi.setTitle(mNewTitle);
}
and then add call the method you just added in your activity:
navView = (NavigationView) findViewById(R.id.navView);
Menu m = navView.getMenu();
for (int i=0;i<m.size();i++) {
MenuItem mi = m.getItem(i);
//for aapplying a font to subMenu ...
SubMenu subMenu = mi.getSubMenu();
if (subMenu!=null && subMenu.size() >0 ) {
for (int j=0; j <subMenu.size();j++) {
MenuItem subMenuItem = subMenu.getItem(j);
applyFontToMenuItem(subMenuItem);
}
}
//the method we have create in activity
applyFontToMenuItem(mi);
}
这篇关于如何更改导航抽屉字体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!