android - 以编程方式更改 BottomNavigationView 文本大小
问题描述
我在我的 android 应用程序中得到了一个,我在这个答案之后BottomNavigationView
设置了自定义文本大小。
<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
编程方式更改文本大小。但是由于我使用尺寸设置字体大小的方式,我现在无法弄清楚如何通过代码更改它。
解决方案
您可以使用此方法:
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);
将标签中的文本大小加倍。
推荐阅读
- javascript - 从 JS 添加跨度
- c# - 如何转换 IEnumerable 的属性
列出 - elasticsearch-5 - 精确搜索的优先级低于语音搜索?
- java - 如何为 Vcard 打开新的 Activity 以及如何打开 Google Maps 应用程序?
- javascript - 大文件上传 - 使用 PHP
- python - AttributeError:模块 'cupy' 没有属性 'array'
- solr - 由于 timeAllowed 参数,识别 SOLR 返回空结果的方法?
- dart - List.sort 返回 null
- python - 如何解决python中的“中断代码”问题?
- python - 熊猫数据框为每日数据提供每周平均值