Android - 如何更改底部导航栏文本的字体系列

Neh*_*wal 27 fonts android android-layout typeface

我在我的android页面中创建了一个底栏导航.但现在我想在底部导航文本中应用自定义字体系列.

这是.xml文件中的底部导航代码:

<android.support.design.widget.BottomNavigationView
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/bottomNavView_Bar"
            android:background="@drawable/white_grey_border_top"
            app:menu="@menu/bottom_navigation_menu">
</android.support.design.widget.BottomNavigationView>
Run Code Online (Sandbox Code Playgroud)

此外,代码bottom_navigation_menu.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
      >

    <item
        android:id="@+id/ic_newsfeed"
        android:icon="@drawable/ic_menu_camera"
        android:title="NEWSFEED"
        />

        <item
            android:id="@+id/ic_explorer"
            android:icon="@drawable/ic_home_black_24dp"
            android:title="EXPLORER"
            />
        <item
            android:id="@+id/ic_notify"
            android:icon="@drawable/ic_notifications_black_24dp"
            android:title="NOTIFY"
            />
        <item
            android:id="@+id/ic_more"
            android:icon="@drawable/ic_dashboard_black_24dp"
            android:title="MORE"
            />

</menu>
Run Code Online (Sandbox Code Playgroud)

帮助将不胜感激.

提前致谢!

A. *_*ets 75

在res/font /文件夹中添加字体文件以将字体捆绑为资源

然后

您可以使用样式资源更改它.在styles.xml中:

<style name="Widget.BottomNavigationView" 
    parent="Widget.Design.BottomNavigationView">
    <item name="fontFamily">@font/your_font</item>
</style>
Run Code Online (Sandbox Code Playgroud)

然后将其作为主题应用于您的视图中:

<android.support.design.widget.BottomNavigationView
    ...
    android:theme="@style/Widget.BottomNavigationView"
/>
Run Code Online (Sandbox Code Playgroud)

刚刚检查我的应用程序,它工作正常.

参考:https: //developer.android.com/guide/topics/ui/look-and-feel/fonts-in-xml.html#fonts-in-code

  • 谢谢。该答案应标记为已接受。就我而言,我使用的是“书法库”,因此我的样式xml类似于“ &lt;item name =“ fontPath”&gt; fonts / lato_medium.ttf &lt;/ item&gt;”。 (3认同)

mon*_*key 14

在你的布局中:

<com.google.android.material.bottomnavigation.BottomNavigationView
    ...
    app:itemTextAppearanceActive="@style/BottomNavigationViewTextStyle"
    app:itemTextAppearanceInactive="@style/BottomNavigationViewTextStyle"
    ... />
Run Code Online (Sandbox Code Playgroud)

在styles.xml中:

<style name="BottomNavigationViewTextStyle">
    ...
    <item name="android:fontFamily">@font/whatever_font</item>
    ...
</style>
Run Code Online (Sandbox Code Playgroud)

  • 仅供参考:这对我来说不适用于 android:fontFamily,仅适用于 fontFamily (相当于 app:fontFamily) (3认同)
  • 这比 A. Kazarovets 的答案更好,因为它不会覆盖和破坏动画 (2认同)

Moh*_*eza 8

如果您在“资产文件夹”中有一个 CustomFont 并且您想在“底部导航”中进行设置,请使用此代码

        public static void persian_iran_font(final Context context, final View v) {
            try {
                if (v instanceof ViewGroup) {
                    ViewGroup vg = (ViewGroup) v;
                    for (int i = 0; i < vg.getChildCount(); i++) {
                        View child = vg.getChildAt(i);
                        persian_iran_font(context, child);
                    }
                } else if (v instanceof TextView) {
                    ((TextView) v).setTypeface(Typeface.createFromAsset(context.getAssets(), "teshrinarmedium.otf"));
                }
            } catch (Exception e) {
            }
        }
    
Run Code Online (Sandbox Code Playgroud)

然后像这样在你的 MainActivity 中使用 methode

  BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
  persian_iran_font(getApplicationContext(), navigation);
Run Code Online (Sandbox Code Playgroud)

kotlin 版本

fun persian_iran_font(context: Context, v: View) {
    try {
        if (v is ViewGroup) {
            val vg = v as ViewGroup
            for (i in 0 until vg.childCount) {
                val child: View = vg.getChildAt(i)
                persian_iran_font(context, child)
            }
        } else if (v is TextView) {
            (v as TextView).setTypeface(
                Typeface.createFromAsset(
                    context.getAssets(),
                    "teshrinarmedium.otf"
                )
            )
        }
    } catch (e: Exception) {
    }
}
Run Code Online (Sandbox Code Playgroud)

祝你好运


Ara*_*ash 7

这可以通过覆盖BottomNavigationView类的onLayout方法然后使用扩展标记来完成.此方法还显示所有菜单标题并禁用移位.

public final class ExtendedBottomNavigationView extends BottomNavigationView{
    private final Context context;
    private Typeface fontFace = null;

    public ExtendedBottomNavigationView(Context context, AttributeSet attrs){
        super(context, attrs);
        this.context = context;
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom){
        super.onLayout(changed, left, top, right, bottom);
        final ViewGroup bottomMenu = (ViewGroup)getChildAt(0);
        final int bottomMenuChildCount = bottomMenu.getChildCount();
        BottomNavigationItemView item;
        View itemTitle;
        Field shiftingMode;

        if(fontFace == null){
            fontFace = Typeface.createFromAsset(context.getAssets(), context.getString(R.string.VazirBold));
        }
        try {
            //if you want to disable shiftingMode:
            //shiftingMode is a private member variable so you have to get access to it like this:
            shiftingMode = bottomMenu.getClass().getDeclaredField("mShiftingMode");
            shiftingMode.setAccessible(true);
            shiftingMode.setBoolean(bottomMenu, false);
            shiftingMode.setAccessible(false);
        } catch (NoSuchFieldException e){
            e.printStackTrace();
        } catch (IllegalAccessException e){e.printStackTrace();}

        for(int i=0; i<bottomMenuChildCount; i++){
            item = (BottomNavigationItemView)bottomMenu.getChildAt(i);
            //this shows all titles of items
            item.setChecked(true);
            //every BottomNavigationItemView has two children, first is an itemIcon and second is an itemTitle
            itemTitle = item.getChildAt(1);
            //every itemTitle has two children, first is a smallLabel and second is a largeLabel. these two are type of AppCompatTextView
            ((TextView)((BaselineLayout) itemTitle).getChildAt(0)).setTypeface(fontFace, Typeface.BOLD);
            ((TextView)((BaselineLayout) itemTitle).getChildAt(1)).setTypeface(fontFace, Typeface.BOLD);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

然后像这样使用它:

<your.package.name.ExtendedBottomNavigationView android:id="@id/bottomMenu" style="@style/bottomMenu"/>
Run Code Online (Sandbox Code Playgroud)


Tar*_*der 7

对于 Kotlin 爱好者

为自定义资源字体创建扩展类

    /**
      * Method for Bottom Navigation Font Family
      *@param view
      *
      * */
private fun navigationTextFont(view: View) {
    if (view is ViewGroup) {
        for (i in 0 until view.childCount) {
            val child = view.getChildAt(i)
            navigationTextFont(child)
        }
    } else if (view is TextView) {
        // font from asset
        view.typeface = Typeface.createFromAsset(this.assets, "fonts/roboto_bold.ttf")
        
        // Or Font From resource
        view.typeface = ResourcesCompat.getFont(this,R.font.roboto_bold)
    }
}
Run Code Online (Sandbox Code Playgroud)

现在调用此扩展程序

   navigationTextFont(yourNavigationViewId)
Run Code Online (Sandbox Code Playgroud)

祝你好运


Bin*_*ink 5

根据https://material.io/develop/android/components/bottom-navigation-view/,只需为 TextAppearance.MaterialComponents.Caption 样式设置一个 fontFamily 项目,它就会正常工作。com.google.android.material.bottomnavigation.BottomNavigationView 将默认使用它。

只知道依赖 TextAppearance.MaterialComponents.Caption 的其他组件也会发生变化,但这可能是可取的。

<style name="TextAppearance.MaterialComponents.Caption" parent="AppMaterialComponentsTheme">
        <item name="fontFamily">@font/example</item>
</style>
Run Code Online (Sandbox Code Playgroud)


And*_*Sco 5

科特林语言:

1:创建一个自定义名称的文件,例如: BottomNavigationViewExtension.kt

2:把代码放在下面:

import android.graphics.Typeface
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import com.google.android.material.bottomnavigation.BottomNavigationView

fun BottomNavigationView.changeNavTypeface(typeface: Typeface) {
    val view: View = this
    checker(view, typeface)

}
private fun checker(view: View, typeface: Typeface) {
    if (view is ViewGroup) {
        for (i in 0 until view.childCount) {
            val child = view.getChildAt(i)
            checker(child, typeface)
        }
    } else if (view is TextView) {
        view.typeface = typeface
    }
}
Run Code Online (Sandbox Code Playgroud)

3:用法:

navView.changeNavTypeface(
    Typeface.createFromAsset(
        assets,
        "fonts/IRANSansMobile.ttf"
    )
)
Run Code Online (Sandbox Code Playgroud)