如何在片段中使用setSupportActionBar [英] how to use setSupportActionBar in fragment

查看:487
本文介绍了如何在片段中使用setSupportActionBar的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在无法使用的片段中使用setSupportActionBar,也无法使用setContentView,请提前提供帮助,谢谢 相关代码已给出

I need to use the setSupportActionBar in fragment which I am unable to also I am unable to use setContentView please to help with it also Thankyou in advance the related code is given

public class StudentrFragment extends Fragment {
        Toolbar toolbar;
        TabLayout tabLayout;
        ViewPager viewPager;
        ViewPagerAdapter viewPagerAdapter;


        public StudentrFragment() {
            // Required empty public constructor
        }


        @Override
        public void onCreate(Bundle savedInstanceState){
            super.onCreate(savedInstanceState);
            setContentView(R.layout.tabbar_layout);
            toolbar = (Toolbar) findViewById(R.id.toolbar);
            setSupportActionBar(toolbar); 
            tabLayout = (TabLayout) findViewById(R.id.tabLayout);
            viewPager = (ViewPager) findViewById(R.id.viewPager);
            viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
            viewPagerAdapter.addFragments(new CivilFragment(),"Civil Dept");
            viewPagerAdapter.addFragments(new ComputerFragment(),"CSE Dept");
            viewPagerAdapter.addFragments(new EeeFragment(),"EEE Dept");
            viewPagerAdapter.addFragments(new EceFragment(),"ECE Dept");
            viewPager.setAdapter(viewPagerAdapter);
            tabLayout.setupWithViewPager(viewPager);

        }

    }

推荐答案

您可以像这样在片段中设置SupportActionbar:

You can setSupportActionbar like this in fragments:

((AppCompatActivity)getActivity()).setSupportActionBar(mToolbar);

您需要为FragmentonCreateView中的tabbar_layout充气. 像这样:

You need to inflate tabbar_layout in onCreateView of Fragment. Like this:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tabbar_layout, container, false);
 //YOUR STUFF
return rootView;
}

这篇关于如何在片段中使用setSupportActionBar的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆