Android 如何动态添加BottomNavigationView

Android 如何动态添加BottomNavigationView,android,bottomnavigationview,Android,Bottomnavigationview,我以编程方式添加了一个BottomNavigationView,但它位于工具栏上方,如何修复它 View parent = (View) container.getParent(); coordinatorLayout = parent.findViewById(R.id.parent_container); bottomNavigationView = new BottomNavigationView(getContext()); BottomNavigationV

我以编程方式添加了一个
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);

这可能是一个评论。如果作为答案发布,请用代码编写解决方案。请用XML代码更新您的问题。