底部导航栏空白
来源:3-5 构建底部导航栏

小奶牛爱学习
2021-01-21
对着视频敲的代码,运行的时候为空白
public class AppBottomBar extends BottomNavigationView {
private static final int[] sIcons = new int[] {
R.drawable.icon_tab_home,
R.drawable.icon_tab_sofa,
R.drawable.icon_tab_publish,
R.drawable.icon_tab_find,
R.drawable.icon_tab_mine
};
public AppBottomBar(@NonNull Context context) {
super(context, null);
}
public AppBottomBar(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs, 0);
}
@SuppressLint("RestrictedApi")
public AppBottomBar(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//获取bottomBar的Json POJO
BottomBar bottomBar = AppConfig.getBottomBar();
//设置底部按钮两种状态的颜色
int[][] states = new int[2][];
states[0] = new int[]{android.R.attr.state_selected};//selected state
states[1] = new int[]{};//default state
int[] colors = new int[] {
Color.parseColor(bottomBar.activeColor), //selected state color
Color.parseColor(bottomBar.inActiveColor)//default state color
};
ColorStateList colorStateList = new ColorStateList(states, colors);
setItemIconTintList(colorStateList);
setItemTextColor(colorStateList);
//LABEL_VISIBILITY_LABELED:设置按钮的文本为一直显示模式
//LABEL_VISIBILITY_AUTO:当按钮个数小于三个时一直显示,或者当按钮个数大于3个且小于5个时,被选中的那个按钮文本才会显示
//LABEL_VISIBILITY_SELECTED:只有被选中的那个按钮的文本才会显示
//LABEL_VISIBILITY_UNLABELED:所有的按钮文本都不显示
setLabelVisibilityMode(LabelVisibilityMode.LABEL_VISIBILITY_LABELED);
setSelectedItemId(bottomBar.selectTab);
List<BottomBar.Tabs> tabs = bottomBar.tabs;
//添加底部导航图标
for (int i = 0; i < tabs.size(); i++) {
BottomBar.Tabs tab = tabs.get(i);
if (!tab.enable)
continue;
int itemId = getItemId(tab.pageUrl);
if (itemId < 0) {
continue;
}
MenuItem item = getMenu().add(0, itemId, tab.index, tab.title);
item.setIcon(sIcons[tab.index]);
}
//修改底部导航图标大小
int index = 0;
for (int i = 0; i < tabs.size(); i++) {
BottomBar.Tabs tab = tabs.get(i);
int iconSize = dp2px(tab.size);
//child是BottomNavigationMenuView
BottomNavigationMenuView menuView = (BottomNavigationMenuView) getChildAt(0);
BottomNavigationItemView itemView = (BottomNavigationItemView) menuView.getChildAt(tab.index);
itemView.setIconSize(iconSize);
//给中间的大按钮着色
if (TextUtils.isEmpty(tab.title)) {
int tintColor = TextUtils.isEmpty(tab.tintColor) ? Color.parseColor("#ff678f") : Color.parseColor(tab.tintColor);
itemView.setIconTintList(ColorStateList.valueOf(tintColor));
/*
* 如果想要禁止掉所有按钮的点击浮动效果。
* 那么还需要给选中和未选中的按钮配置一样大小的字号。
*
* 在MainActivity布局的AppBottomBar标签增加如下配置,
* @style/active,@style/inActive 在style.xml中
* app:itemTextAppearanceActive="@style/active"
* app:itemTextAppearanceInactive="@style/inActive"
*/
itemView.setShifting(false);
}
index++;
}
//底部导航栏默认选中项
if (bottomBar.selectTab != 0) {
BottomBar.Tabs selectTab = bottomBar.tabs.get(bottomBar.selectTab);
if (selectTab.enable) {
int itemId = getItemId(selectTab.pageUrl);
//这里需要延迟一下 再定位到默认选中的tab
//因为 咱们需要等待内容区域,也就NavGraphBuilder解析数据并初始化完成,
//否则会出现 底部按钮切换过去了,但内容区域还没切换过去
post(() -> setSelectedItemId(itemId));
}
}
}
private int dp2px(int size) {
float value = getContext().getResources().getDisplayMetrics()
.density * size + 0.5f;
return (int) value;
}
private int getItemId(String pageUrl) {
Destination destination = AppConfig.getsDestConfig().get(pageUrl);
if (destination == null) {
return -1;
}
return destination.id;
}
}
写回答
2回答
-
断点看下,你的tabs集合的数据,是否执行了for循环
012021-01-22 -
小奶牛爱学习
提问者
2021-01-22
构造器调用了super,还需要根据
main_tabs_config.json配置相同数量的Fragment10
相似问题