如何动态添加BottomNavigationView

问题描述 投票:0回答:1

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

android bottomnavigationview
1个回答
-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);
© www.soinside.com 2019 - 2024. All rights reserved.