如何更改导航抽屉字体?

问题描述 投票:12回答:6

我想在android.I中使用我自己的字体导航抽屉。根据这个答案使用android自带的工作室:https://stackoverflow.com/a/23632492/4393226。但我不知道如何更改字体并使其成为RTL。我搜索了很多,我发现了如何制作抽屉RTL。我用这个代码:

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);

Android - Is Navigation Drawer from right hand side possible?

但正如您所知,这仅适用于API 17及更高版本。请帮忙!如何更改菜单字体?如何以正确的方式进行布局RTL?!

编辑:我的字体“TTF”文件是资产/字体,我知道如何使用java设置文本视图,但我不知道如何将其设置为导航抽屉菜单。

android fonts navigation-drawer right-to-left
6个回答
36
投票

我找到了答案:首先在你的项目中创建这个类:

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;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);
}

18
投票

您可以更轻松地完成此操作。

首先进入style.xml并创建一个新样式。

 <style name="RobotoTextViewStyle" parent="android:Widget.TextView">
      <item name="android:fontFamily">@font/sans-serif-smallcaps</item>
 </style>

第二个是转到导航xml代码并将项目文本外观放在那里。

app:itemTextAppearance="@style/RobotoTextViewStyle"

现在你的最终导航代码应该是这个。

<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" />

10
投票

第1步:在style.xml中按如下方式创建样式

<style name="NavigationView" >
<item name="fontFamily">@font/helvetica_neue_light</item>
</style>

第2步:在android.support.design.widget.NavigationView中添加样式作为主题

机器人:主题= “@风格/ NavigationView”


9
投票

添加到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);

    }

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);
    }
}

无法相信改变菜单字体的复杂程度。


5
投票

谢谢!我已经成功更改了基于@Amir H post的导航抽屉上的字体但是配置了(只需在您的活动中添加这几行)

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);
                }
            }

        }

也许它会帮助别人:)


2
投票

它仅适用于字体

  1. 首先在位于colors.xmlres->values->colors.xml文件中添加您的字体颜色(如果您想更改) <color name="black">#000000</color> // it's for black don't go for white color
  2. 然后编辑位于相同值目录的style.xml文件(有两个文件编辑该文件,其主题为name="your_theme"样式或在该两个文件中查找行
  3. 这里我们要设置font属性。因此,您必须在封闭资源标记中创建新的样式标记。在我的情况下,我创造 <style name="MyText" parent="@android:style/TextAppearance.Medium"> <item name="android:textSize">20sp</item> //size of font <item name="android:textColor">@color/black</item> //color of font <item name="android:typeface">sans</item> // type how it appear </style> 请注意,为此标记指定的名称是MyText。现在我们必须在上面的第一个样式块中使用此名称,其名称是您的应用主题。
  4. 在上面的appication主题样式标签中提到了这种新风格。就我而言,它就像 <!-- Base application theme. --> <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar"> <!-- Customize your theme here. --> <item name="colorPrimary">@color/colorPrimary</item> <item name="colorPrimaryDark">@color/colorPrimaryDark</item> <item name="colorAccent">@color/colorAccent</item> <item name="android:textViewStyle">@style/MyText</item> //MyText its custom style for font </style>
© www.soinside.com 2019 - 2024. All rights reserved.