Android 以编程方式更改BottomNavigationView文本大小

Android 以编程方式更改BottomNavigationView文本大小,android,bottomnavigationview,Android,Bottomnavigationview,我在android应用程序中获得了一个BottomNavigationView,我设置了自定义文本大小 25sp 22便士 现在,由于我在这个应用程序中实现了多种语言,我需要以编程方式更改BottomNavigationView的文本大小。但是,由于我使用dimens设置字体大小的方式,我现在不知道如何通过代码进行更改。您可以使用以下方法: private void setBottomNavigationLabelsTextSize(BottomNavigationView bottomNav

我在android应用程序中获得了一个
BottomNavigationView
,我设置了自定义文本大小

25sp
22便士

现在,由于我在这个应用程序中实现了多种语言,我需要以编程方式更改
BottomNavigationView
的文本大小。但是,由于我使用dimens设置字体大小的方式,我现在不知道如何通过代码进行更改。

您可以使用以下方法:

private void setBottomNavigationLabelsTextSize(BottomNavigationView bottomNavigationView, float ratio) {
    for (int i = 0; i < bottomNavigationView.getChildCount(); i++) {
        View item = bottomNavigationView.getChildAt(i);

        if (item instanceof BottomNavigationMenuView) {
            BottomNavigationMenuView menu = (BottomNavigationMenuView) item;

            for (int j = 0; j < menu.getChildCount(); j++) {
                View menuItem = menu.getChildAt(j);

                View small = menuItem.findViewById(android.support.design.R.id.smallLabel);
                if (small instanceof TextView) {
                    float size = ((TextView) small).getTextSize();
                    ((TextView) small).setTextSize(TypedValue.COMPLEX_UNIT_PX, ratio * size);
                }
                View large = menuItem.findViewById(android.support.design.R.id.largeLabel);
                if (large instanceof TextView) {
                    float size = ((TextView) large).getTextSize();
                    ((TextView) large).setTextSize(TypedValue.COMPLEX_UNIT_PX, ratio * size);
                }
            }
        }
    }
}
将标签中文本的大小增加一倍

private void setBottomNavigationLabelsTextSize(BottomNavigationView bottomNavigationView, float ratio) {
    for (int i = 0; i < bottomNavigationView.getChildCount(); i++) {
        View item = bottomNavigationView.getChildAt(i);

        if (item instanceof BottomNavigationMenuView) {
            BottomNavigationMenuView menu = (BottomNavigationMenuView) item;

            for (int j = 0; j < menu.getChildCount(); j++) {
                View menuItem = menu.getChildAt(j);

                View small = menuItem.findViewById(android.support.design.R.id.smallLabel);
                if (small instanceof TextView) {
                    float size = ((TextView) small).getTextSize();
                    ((TextView) small).setTextSize(TypedValue.COMPLEX_UNIT_PX, ratio * size);
                }
                View large = menuItem.findViewById(android.support.design.R.id.largeLabel);
                if (large instanceof TextView) {
                    float size = ((TextView) large).getTextSize();
                    ((TextView) large).setTextSize(TypedValue.COMPLEX_UNIT_PX, ratio * size);
                }
            }
        }
    }
}
setBottomNavigationLabelsTextSize(bottomNavigationView, 2.0f);