Inflating AppBarLayout with Toolbar + TabLayout

前端 未结 7 1180
隐瞒了意图╮
隐瞒了意图╮ 2021-01-30 23:21

I currently have a DrawerLayout in my main.xml. There\'s a Toolbar wrapped in an AppBarLayout, and then a simple LinearLayout to swap out fragments.

One of the fragment

7条回答
  •  感动是毒
    2021-01-30 23:28

    You can simply add TabLayout programmatically from Fragment in wich you need TabLayout

    tabLayout = (TabLayout) inflater.inflate(R.layout.tablay, null);
    appBarLayout = (AppBarLayout) getActivity().findViewById(R.id.appbar);
    appBarLayout.addView(tabLayout, new LinearLayoutCompat.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
    

    and remove TabLayout from AppBar in onDetach()

    @Override
    public void onDetach() {
        appBarLayout.removeView(tabLayout);
        super.onDetach();
    }
    

提交回复
热议问题