Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
335 views
in Technique[技术] by (71.8m points)

android - PagerTabStrip tabs alignment to left

I am using PagerTabStrip to set sliding tabs. However, when I set three tabs, it shows the first tab in the center so users cannot see all three tabs at first glance.

How can I align them to left?

enter image description here

I want to move tabs to the left like this.

enter image description here

I tried gravity, but no luck.

pagerTabStrip.setGravity(Gravity.LEFT);

This is the code for MainActivity.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pager_tab_strip);

    FragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(
            getSupportFragmentManager());
    viewPager.setAdapter(fragmentPagerAdapter);

    pagerTabStrip.setGravity(Gravity.LEFT);
}

And

MyFragmentPagerAdapter extends FragmentPagerAdapter {

    private final String[] pageTitle = { "Entry", "History", "Report" };

    public MyFragmentPagerAdapter(FragmentManager fragmentManager) {
        super(fragmentManager);
    }

    @Override
    public Fragment getItem(int position) {

        Fragment fragment = new PageFragment();
        Bundle arguments = new Bundle();
        arguments.putString("pageIndex", Integer.toString(position + 1));
        fragment.setArguments(arguments);
        return fragment;
    }

    @Override
    public int getCount() {
        return pageTitle.length;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return pageTitle[position];
    }

}

The Problem Solved!!

You need to download the latest codes from github instead of google developer page.

SlidingTabLayout.java

SlidingTabStrip

Then, you can use setDistributeEvenly(true) like this.

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {

    // Get the ViewPager and set it's PagerAdapter so that it can display items
    mViewPager = (ViewPager) view.findViewById(R.id.viewpager);

    FragmentManager fragmentManager = getFragmentManager();
    MyFragmentPagerAdapter adapter = new MyFragmentPagerAdapter(fragmentManager);
    mViewPager.setAdapter(adapter);

    // Give the SlidingTabLayout the ViewPager, this must be done AFTER the ViewPager has had
    // it's PagerAdapter set.
    mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
    mSlidingTabLayout.setDistributeEvenly(true);
    mSlidingTabLayout.setViewPager(mViewPager);
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Use this before setViewPager():

sliding_tabs.setDistributeEvenly(true);

If you got this error Cannot resolve method 'setDistributeEvenly(boolean)', you should update your SlidingTabLayout.java and SlidingTabStrip.java from Google I/O source code:

SlidingTabLayout.java

SlidingTabStrip


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...