我正在使用PagerTabStrip来设置滑动标签。但是,当我设置三个选项卡时,它会显示中心的第一个选项卡,这样用户一眼就看不到所有三个选项卡。
如何将它们对齐到左边?
我想像这样向左移动标签。
我试过引力,但没有运气。
pagerTabStrip.setGravity(Gravity.LEFT);
这是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);
}
和
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];
}
}
您需要从github而不是谷歌开发者页面下载最新代码。
然后,您可以像这样使用setDistributeEvenly(true)。
@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);
}
答案 0 :(得分:12)
在setViewPager()之前使用它:
sliding_tabs.setDistributeEvenly(true);
如果您收到此错误Cannot resolve method 'setDistributeEvenly(boolean)'
,则应从Google I / O源代码更新SlidingTabLayout.java和SlidingTabStrip.java: