Ami*_*r H 15 fonts android right-to-left navigation-drawer
我想在android中使用我自己的字体导航抽屉.我根据这个答案使用了android studio附带的库:https://stackoverflow.com/a/23632492/4393226.但我不知道如何更改字体并使其成为RTL.我搜索了很多,我发现了如何制作抽屉RTL.我用这个代码:
getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
Run Code Online (Sandbox Code Playgroud)
但正如您所知,这仅适用于API 17及更高版本.请帮忙!如何更改菜单字体?如何以正确的方式进行布局RTL?!
编辑:我的字体"TTF"文件是资产/字体,我知道如何使用java设置文本视图,但我不知道如何将其设置为导航抽屉菜单.
Ami*_*r H 37
我找到了答案:首先在你的项目中创建这个类:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
然后将此方法添加到您想要更改导航抽屉菜单字体的活动中:
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);
}
Run Code Online (Sandbox Code Playgroud)
然后添加调用您刚刚在活动中添加的方法:
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);
}
Run Code Online (Sandbox Code Playgroud)
Shr*_*kur 20
您可以以更简单的方式执行此操作。
首先进入style.xml并在那里创建新样式。
<style name="RobotoTextViewStyle" parent="android:Widget.TextView">
<item name="android:fontFamily">@font/sans-serif-smallcaps</item>
</style>
Run Code Online (Sandbox Code Playgroud)
第二个是转到您的导航xml代码,并将项目文本外观放在此处。
app:itemTextAppearance="@style/RobotoTextViewStyle"
Run Code Online (Sandbox Code Playgroud)
现在您的最终导航代码应该是这个。
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextAppearance="@style/RobotoTextViewStyle"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_home"
app:menu="@menu/activity_home_drawer" />
Run Code Online (Sandbox Code Playgroud)
小智 10
第1步:在style.xml中按如下方式创建样式
<style name="NavigationView" >
<item name="fontFamily">@font/helvetica_neue_light</item>
</style>
Run Code Online (Sandbox Code Playgroud)
第2步:在android.support.design.widget.NavigationView中添加样式作为主题
机器人:主题= "@风格/ NavigationView"
添加到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;i<m.size();i++) {
MenuItem mi = m.getItem(i);
SpannableString s = new SpannableString(mi.getTitle());
s.setSpan(new CustomTypefaceSpan("", tf1), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
mi.setTitle(s);
}
Run Code Online (Sandbox Code Playgroud)
CustomTypefaceSpan类:
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);
}
}
Run Code Online (Sandbox Code Playgroud)
无法相信改变菜单字体的复杂程度.
小智 5
谢谢!我已基于@Amir H帖子成功更改了导航抽屉上的字体,但已进行了配置(只需将这几行添加到您的活动中)
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Menu m = navigationView .getMenu();
for (int i=0;i<m.size();i++) {
MenuItem mi = m.getItem(i);
//for applying 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);
SpannableString s = new SpannableString(subMenuItem.getTitle());
s.setSpan(new TypefaceSpan("fonts/yourfontname.ttf"), 0, s.length(),
Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
subMenuItem.setTitle(s);
}
}
}
Run Code Online (Sandbox Code Playgroud)
也许会帮助某人:)