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

时间:2018-12-25 17:13:24

标签: android bottomnavigationview

我在自己的Android应用中设置了自定义文本大小following this answerBottomNavigationView

<dimen name="design_bottom_navigation_text_size" tools:override="true">25sp</dimen>
<dimen name="design_bottom_navigation_active_text_size" tools:override="true">22sp</dimen>

现在,由于我在此应用中实现了多种语言,因此需要以编程方式更改BottomNavigationView的文本大小。但是,由于我通过减暗设置字体大小的方式,所以我现在不知道如何通过代码更改字体大小。

感谢您以后提出的提示或解决方案。

1 个答案:

答案 0 :(得分:0)

您可以使用此方法:

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

将标签中文本的大小加倍。

相关问题