如何改变导航抽屉的字体?

30 人关注

我想在安卓系统中使用我自己的字体做导航抽屉。我根据这个答案使用了安卓工作室的库。 https://stackoverflow.com/a/23632492/4393226 . 但我不知道如何改变字体并使其成为RTL。 我搜索了很多,找到了如何使抽屉变成RTL的方法。我使用了这个代码。

getWindow().getDecorView().setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
安卓 - 从右手边的导航抽屉是否可以?

但如你所知,这只在API 17及以上版本中有效。 请帮助我们!如何改变菜单的字体?如何以正确的方式使布局RTL化?

Edited: 我的字体 "TTF "文件在assets/fonts中,我知道如何用java将其设置为文本视图,但我不知道如何将其设置为导航抽屉菜单。

2 个评论
你是否使用了一个自定义的xml行来管理菜单......然后我可以告诉你如何改变你的菜单文本字体
@Tanimreja 是的,我做到了!
android
fonts
navigation-drawer
right-to-left
Amir H
Amir H
发布于 2016-01-05
7 个回答
Shrawan Thakur
Shrawan Thakur
发布于 2022-12-24
已采纳
0 人赞同

你可以用更简单的方法做到这一点。

首先进入style.xml,在那里创建一个新的样式。

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

第二个是去你的导航的xml代码,并把项目文本的外观 there.

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" />
    
我希望我能投两次赞成票,这不应该是一个代码解决方案......但xml
先生,你是一个传奇。非常感谢你的回答
Yes, you deserve twice upvote.
Amir H
Amir H
发布于 2022-12-24
0 人赞同

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

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);
    
我的意思是将其设置为RTL。但对于所有的API,而不仅仅是17和以上的API,如何将方向从左到右改变?
@HosseinMansouri 这种情况下,你必须在你的项目中手动添加抽屉,而不是使用谷歌导航抽屉库。
有任何抽屉的样本吗?
@HosseinMansouri 当然可以,谷歌一下就知道了。)
Mahesh Kumar Prajapati
Mahesh Kumar Prajapati
发布于 2022-12-24
0 人赞同

第1步:在style.xml中做出如下风格

<style name="NavigationView" >
<item name="fontFamily">@font/helvetica_neue_light</item>
</style>
将风格作为主题添加在你的
中添加风格作为主题,并将其添加到Android.support.design.widget.NavigationView中。

android:theme="@style/NavigationView"

Ali Khaki
谢谢你,它的工作非常好,而且很简单,不需要创建任何其他类!!!!!。
没有可以继承的父级样式?
更新:不对,不需要父级样式。奇怪的是,安卓在这方面是不一致的。
williamsi
williamsi
发布于 2022-12-24
0 人赞同

对rischan的回答进行补充。

我直接编辑了'mi',因为这些是我的抽屉菜单标题。然后我改变了s.setSpan的第一个参数,使用一个自定义的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);

不敢相信,仅仅是改变菜单的字体就如此复杂。

+1自定义字体的努力,并为子菜单添加字体,这样它将是总导航菜单的明确答案。
rischan
rischan
发布于 2022-12-24
0 人赞同

谢谢!我已经根据@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);

也许它可以帮助别人 :)

Abhishek
Abhishek
发布于 2022-12-24
0 人赞同

It Works For Fonts Only

  • 首先,在位于 res->values->colors.xml colors.xml 文件中添加你的字体颜色(如果你想改变),例如

    <color name="black">#000000</color> // it's for black don't go for white color
    
  • 然后编辑位于同一价值目录下的style.xml文件(有两个文件,编辑具有你的主题风格的name="your_theme"的文件,或在这两个文件中找到一行

  • 这里我们必须设置字体属性。因此,你必须在包围的资源标签中创建新的样式标签。在我的例子中,我创建了

    <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。现在我们必须在第一个样式块中使用这个名字,这个样式块的名字就是你的应用主题。

  • 在上面的应用主题风格标签中提到了这个新风格。在我的例子中,它就像

    <!-- 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>
        
  • @Abishek 它没有改变字体。它只改变了大小和颜色。当然,也没有改变导航抽屉!!!!。此外,我的字体文件是在assets/fonts文件夹中。
    Sudarsan panda
    Sudarsan panda
    发布于 2022-12-24
    0 人赞同

    first, create an XML file in under res>>values with a name style.xml

    然后复制并在 <resource><style name="mynavigationfont" > <item name="fontFamily">@font/sans-serif</item> </style> </resource> 下添加此代码

    Note: - add this code in <resource ... here .. if >

    <?xml version="1.0" encoding="UTF-8"?>
    

    如果你不添加这个,它可能会显示错误

    最后,在你的XML中加入以下内容

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