【问题标题】:How to change navigation drawer font?如何更改导航抽屉字体?
【发布时间】:2016-01-05 12:22:07
【问题描述】:

我想在 android 中使用我自己的导航抽屉字体。我根据这个答案使用 android studio 附带的库: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”文件在 assets/fonts 中,我知道如何使用 java 为 textview 设置它,但我不知道如何将它设置为导航抽屉菜单。

【问题讨论】:

  • 您是否使用自定义 xml 行来管理菜单.. 那么我可以告诉您如何更改菜单文本字体
  • @Tanimreja 是的,我做到了!

标签: android fonts navigation-drawer right-to-left


【解决方案1】:

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

首先进入 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" />

【讨论】:

  • 希望我能投票两次,这不应该是代码解决方案...但是 xml
  • 先生,您是传奇。感谢一百万的回答
【解决方案2】:

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

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

然后添加调用你刚才在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);
}

【讨论】:

  • 优秀。但是如何从左到右改变方向。我的意思是将其设置为 RTL 。但对于所有 API,不仅适用于 17 及以上?
  • @HosseinMansouri 在这种情况下,您必须手动在项目中添加抽屉,而不是使用谷歌导航抽屉库。
  • 任何抽屉样品?
  • @HosseinMansouri 当然可以,谷歌一下。 :))
【解决方案3】:

第一步:在style.xml中制作如下样式

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

第 2 步: 将样式添加为您的主题 android.support.design.widget.NavigationView

android:theme="@style/NavigationView"

【讨论】:

  • 感谢它工作得非常好和简单,无需创建任何其他类!!!!!
  • 没有可以继承的父样式?
  • 更新:不,不需要父样式。奇怪的是 Android 在这方面的不一致。
【解决方案4】:

添加到 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 用于自定义字体工作并为子菜单添加字体。这样就可以为总导航菜单提供明确的答案。
【解决方案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);
                }
            }

        }

也许它会帮助某人:)

【讨论】:

    【解决方案6】:

    仅适用于字体

    1. 首先在位于res-&gt;values-&gt;colors.xmlcolors.xml 文件中添加字体颜色(如果您想更改),就像

      <color name="black">#000000</color> // it's for black don't go for white color
      
    2. 然后编辑位于相同值目录下的style.xml 文件(有两个文件编辑该文件,其主题为name="your_theme" 或在这两个文件中查找行

    3. 这里我们要设置字体属性。所以你必须在封闭的资源标签中创建新的样式标签。就我而言,我创建

      <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. 在上面的应用主题样式标签中提到了这种新样式。就我而言,就像

      <!-- 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 文件夹中。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多