我想在android中使用我自己的字体导航抽屉.我根据这个答案使用了android studio附带的库:https://stackoverflow.com/a/23632492/4393226.但我不知道如何更改字体并使其成为RTL.我搜索了很多,我发现了如何制作抽屉RTL.我用这个代码:
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
和 Android - 右侧的导航抽屉可能吗?
但正如您所知,这仅适用于API 17及更高版本.请帮忙!如何更改菜单字体?如何以正确的方式进行布局RTL?!
编辑:我的字体"TTF"文件是资产/字体,我知道如何使用java设置文本视图,但我不知道如何将其设置为导航抽屉菜单.
我找到了答案:首先在你的项目中创建这个类:
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); } }
然后将此方法添加到您想要更改导航抽屉菜单字体的活动中:
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); }
然后添加调用您刚刚在活动中添加的方法:
navView = (NavigationView) findViewById(R.id.navView); Menu m = navView.getMenu(); for (int i=0;i0 ) { for (int j=0; j
2> 小智..:您可以以更简单的方式执行此操作。
首先进入style.xml并在那里创建新样式。
第二个是转到您的导航xml代码,并将项目文本外观放在此处。
app:itemTextAppearance="@style/RobotoTextViewStyle"现在您的最终导航代码应该是这个。
3> 小智..:第1步:在style.xml中按如下方式创建样式
第2步:在android.support.design.widget.NavigationView中添加样式作为主题
机器人:主题= "@风格/ NavigationView"
4> williamsi..:添加到rischan的答案.
我直接编辑了'mi',因为那些是我的抽屉菜单标题.然后我更改了s.setSpan 1st参数以使用自定义类CustomTypefaceSpan.
// Navigation View NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); Menu m = navigationView .getMenu(); Typeface tf1 = Typeface.createFromAsset(getAssets(), "font/Gotham Narrow Extra Light.otf"); for (int i=0;iCustomTypefaceSpan类:
package my.app; 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); } }无法相信改变菜单字体的复杂程度.
5> 小智..:谢谢!我已基于@Amir H帖子成功更改了导航抽屉上的字体,但已进行了配置(只需将这几行添加到您的活动中)
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); Menu m = navigationView .getMenu(); for (int i=0;i0 ) { for (int j=0; j 也许会帮助某人:)