Android - How to change bottom navigation bar text's font family

后端 未结 11 1493
北荒
北荒 2020-12-14 00:27

I have created a bottom bar navigation in my android page. But now I want to apply the custom font-family in bottom navigation texts.

This is the bottom navigation c

相关标签:
11条回答
  • 2020-12-14 01:04
    public void setNavigationTypeface() {
            final Typeface avenirHeavy = Typeface.createFromAsset(this.getAssets(), "font2/Avenir-Heavy.ttf"); //replace it with your own font
            ViewGroup navigationGroup = (ViewGroup) bottomNavView.getChildAt(0);
            for (int i = 0; i < navigationGroup.getChildCount(); i++) {
                ViewGroup navUnit = (ViewGroup) navigationGroup.getChildAt(i);
                for (int j = 0; j < navUnit.getChildCount(); j++) {
                    View navUnitChild = navUnit.getChildAt(j);
                    if (navUnitChild instanceof BaselineLayout) {
                        BaselineLayout baselineLayout = (BaselineLayout) navUnitChild;
                        for (int k = 0; k < baselineLayout.getChildCount(); k++) {
                            View baselineChild = baselineLayout.getChildAt(k);
                            if (baselineChild instanceof TextView) {
                                TextView textView = (TextView) baselineChild;
                                textView.setTypeface(avenirHeavy);
                            }
                        }
                    }
                }
            }
        }
    
    0 讨论(0)
  • 2020-12-14 01:09

    For Kotlin Lover

    Create an extension class for custom asset font

        /**
          * 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) {
            view.typeface = Typeface.createFromAsset(this.assets, "fonts/roboto_bold.ttf")
        }
    }
    

    Now call this extension

       navigationTextFont(yourNavigationViewId)
    

    Good Luck

    0 讨论(0)
  • 2020-12-14 01:10

    If you want to change the text selection dynamically on selection, you can do bottomNav.itemTextAppearanceActive = R.style.yourstyle

    0 讨论(0)
  • 2020-12-14 01:15

    Per https://material.io/develop/android/components/bottom-navigation-view/, just set a fontFamily item for the TextAppearance.MaterialComponents.Caption style and it’ll Just Work. com.google.android.material.bottomnavigation.BottomNavigationView will use this by default.

    Just know other components that rely on TextAppearance.MaterialComponents.Caption will get changed as well, but this might be desirable.

    <style name="TextAppearance.MaterialComponents.Caption" parent="AppMaterialComponentsTheme">
            <item name="fontFamily">@font/example</item>
    </style>
    
    0 讨论(0)
  • 2020-12-14 01:16

    add the font file in the res/font/ folder to bundle fonts as resources

    then

    You can change it using style resources. In your styles.xml:

    <style name="Widget.BottomNavigationView" 
        parent="Widget.Design.BottomNavigationView">
        <item name="fontFamily">@font/your_font</item>
    </style>
    

    Then apply it as a theme in your view:

    <android.support.design.widget.BottomNavigationView
        ...
        android:theme="@style/Widget.BottomNavigationView"
    />
    

    Just checked on my app, it works fine.

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

    0 讨论(0)
  • 2020-12-14 01:17

    This can be done by overriding onLayout method of BottomNavigationView class then using the extended tag. This approach also shows all menu titles and disables shifting.

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

    Then use it like this:

    <your.package.name.ExtendedBottomNavigationView android:id="@id/bottomMenu" style="@style/bottomMenu"/>
    
    0 讨论(0)
提交回复
热议问题