我想在tablayout中的片段类中设置actionbar颜色和标题

时间:2017-07-04 11:08:28

标签: android android-actionbar

这是我的tabhost Activity,我想在actionbar上设置每个片段标题。我的标签位于xml的底部,我希望每个标签标题位于actionbar的顶部。我们可以在这个tabhostActivity中编写代码吗?或者可以编写任何代码在每个片段中。任何人都建议任何选择吗?

public class TabHostActivity extends AppCompatActivity {
private Toolbar toolbar;
private TabLayout tabLayout;
private ViewPager viewPager;
private int[] tabIcons = {
        R.drawable.hm_icon,
        R.drawable.a_icon,
        R.drawable.user_icon_white,
        R.drawable.gear,
        R.drawable.mail_icon_white
};
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tabs);

    toolbar = (Toolbar) findViewById(R.id.toolbar);

   /* setSupportActionBar(toolbar);*/
    /*getSupportActionBar().setDisplayHomeAsUpEnabled(true);*/

    viewPager = (ViewPager) findViewById(R.id.viewpager);
    setupViewPager(viewPager);


    tabLayout = (TabLayout) findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(viewPager);
    setupTabIcons();


    permissionCheck = ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_CONTACTS);


    if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_CONTACTS}, REQUEST_READ_PHONE_STATE);

    }
}
private void setupTabIcons() {
    tabLayout.getTabAt(0).setIcon(tabIcons[0]);
    tabLayout.getTabAt(1).setIcon(tabIcons[1]);
    tabLayout.getTabAt(2).setIcon(tabIcons[2]);
    tabLayout.getTabAt(3).setIcon(tabIcons[3]);
    tabLayout.getTabAt(4).setIcon(tabIcons[4]);

}

private void setupViewPager(ViewPager viewPager) {
    ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
    adapter.addFrag(new HomeMain(), "");
    adapter.addFrag(new ChooseDefault(), "");
    adapter.addFrag(new MyAccountActivity(), "");
    adapter.addFrag(new UtitlityFragment(), "");
    adapter.addFrag(new SMSFragment(), "");
    viewPager.setAdapter(adapter);
}


class ViewPagerAdapter extends FragmentPagerAdapter {
    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public ViewPagerAdapter(FragmentManager manager) {
        super(manager);
    }

    @Override
    public Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFrag(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }
}

2 个答案:

答案 0 :(得分:1)

您可以尝试这样的方法来获得理想的结果。

YourTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() 
{
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        int pos = tab.getPosition();
        String title = YourFragmentTitle.get(pos);
        YourActionBarObject.setTitle(title);
    }
}

答案 1 :(得分:0)

您可以使用TabLayout.OnTabSelectedListener

mTabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            final int position = tab.getPosition();
            final String title = mFragmentTitleList.get(position);
            mActionBar.setTitle(titleRes);
        }
    }