如何动态添加BottomNavigationView

时间:2018-03-06 06:38:42

标签: android 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);

Here is a screenshot

1 个答案:

答案 0 :(得分:-1)

由于视图已添加到coordinatorLayout,您应该为BottomNavigationView添加CoordinatorLayoutParams。

CoordinatorLayout.LayoutParams layoutParams = new CoordinatorLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.BOTTOM;

    bottomNavigationView.setLayoutParams(layoutParams);