如何动态添加 BottomNavigationView
How to dynamically add a BottomNavigationView
我以编程方式添加了一个 BottomNavigationView
,但它位于工具栏上方,我该如何修复它?
View parent = (View) container.getParent();
coordinatorLayout = parent.findViewById(R.id.parent_container);
bottomNavigationView = new BottomNavigationView(getContext());
BottomNavigationView.LayoutParams params = new BottomNavigationView.LayoutParams(BottomNavigationView.LayoutParams.MATCH_PARENT, BottomNavigationView.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
bottomNavigationView.setLayoutParams(params);
bottomNavigationView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
bottomNavigationView.inflateMenu(R.menu.bottom_navigation_main);
coordinatorLayout.addView(bottomNavigationView, params);
既然视图被添加到coordinatorLayout,你应该为BottomNavigationView添加CoordinatorLayoutParams。
CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.BOTTOM;
bottomNavigationView.setLayoutParams(layoutParams);
我以编程方式添加了一个 BottomNavigationView
,但它位于工具栏上方,我该如何修复它?
View parent = (View) container.getParent();
coordinatorLayout = parent.findViewById(R.id.parent_container);
bottomNavigationView = new BottomNavigationView(getContext());
BottomNavigationView.LayoutParams params = new BottomNavigationView.LayoutParams(BottomNavigationView.LayoutParams.MATCH_PARENT, BottomNavigationView.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
bottomNavigationView.setLayoutParams(params);
bottomNavigationView.setBackgroundColor(getResources().getColor(R.color.colorPrimaryDark));
bottomNavigationView.inflateMenu(R.menu.bottom_navigation_main);
coordinatorLayout.addView(bottomNavigationView, params);
既然视图被添加到coordinatorLayout,你应该为BottomNavigationView添加CoordinatorLayoutParams。
CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.BOTTOM;
bottomNavigationView.setLayoutParams(layoutParams);