Inflating AppBarLayout with Toolbar + TabLayout

前端 未结 7 1042
隐瞒了意图╮
隐瞒了意图╮ 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:46

    I had a similar issue where I wanted a TabLayout just inside of one fragment.

    Without changing any other code you can solve this by using onAttach and onDetach inside your fragment with the TabLayout.

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    
        // todo add some further checks, e.g. instanceof, actionbar != null
    
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ((AppCompatActivity) activity).getSupportActionBar().setElevation(0);
        }
    }
    
    @Override
    public void onDetach() {
        super.onDetach();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            ((AppCompatActivity) getActivity()).getSupportActionBar()
                    .setElevation(getResources().getDimension(R.dimen.toolbar_elevation));
        }
    }
    

    Be sure to set the same elevation on your TabLayout and everything works fine! ;D

    0 讨论(0)
提交回复
热议问题