[英]Change BottomNavigationView text size programmatically
我在我的 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);
將標簽中的文本大小加倍。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.