问题描述
我使用PagerTabStrip设置滑动标签。但是,当我设置三个选项卡,它显示在中央的第一个选项卡,使用户无法看到所有三个选项卡的第一眼。
我该如何调整他们离开?
我想移动的标签左侧是这样的。
我试过了重力,但没有运气。
pagerTabStrip.setGravity(Gravity.LEFT);
这是在$ C $下MainActivity。
@覆盖
保护无效的onCreate(包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 =新MyFragmentPagerAdapter(
getSupportFragmentManager());
viewPager.setAdapter(fragmentPagerAdapter);
pagerTabStrip.setGravity(Gravity.LEFT);
}
和
MyFragmentPagerAdapter延伸FragmentPagerAdapter {
私人最终的String [] PAGETITLE = {入口,历史,报告};
公共MyFragmentPagerAdapter(FragmentManager fragmentManager){
超(fragmentManager);
}
@覆盖
公共片段的getItem(INT位置){
片段片段=新PageFragment();
捆绑参数=新包();
arguments.putString(PageIndex的,Integer.toString(位置+ 1));
fragment.setArguments(参数);
返回片段;
}
@覆盖
公众诠释getCount将(){
返回pageTitle.length;
}
@覆盖
公共CharSequence的getPageTitle(INT位置){
返回PAGETITLE [位置]
}
}
的问题就迎刃而解了!
您需要下载从GitHub上,而不是谷歌开发者页面获取最新的codeS。
SlidingTabLayout.java
SlidingTabStrip
然后,您可以使用setDistributeEvenly(真)是这样的。
@覆盖
公共无效onViewCreated(查看视图,捆绑savedInstanceState){
//获取ViewPager并设置它的PagerAdapter以便它可以显示项目
mViewPager =(ViewPager)view.findViewById(R.id.viewpager);
FragmentManager fragmentManager = getFragmentManager();
MyFragmentPagerAdapter适配器=新MyFragmentPagerAdapter(fragmentManager);
mViewPager.setAdapter(适配器);
//给这个SlidingTabLayout的ViewPager,这是一定要做的后ViewPager已
//这是PagerAdapter集。
mSlidingTabLayout =(SlidingTabLayout)view.findViewById(R.id.sliding_tabs);
mSlidingTabLayout.setDistributeEvenly(真正的);
mSlidingTabLayout.setViewPager(mViewPager);
}
setViewPager()之前,使用这样的:
sliding_tabs.setDistributeEvenly(真正的);
如果你得到这个错误无法从谷歌I / O源$ C解决方法setDistributeEvenly(布尔)
,您应该更新您的SlidingTabLayout.java和SlidingTabStrip.java $ C:
SlidingTabLayout.java
SlidingTabStrip
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?
I want to move tabs to the left like this.
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.
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);
}
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:
这篇关于PagerTabStrip标签定位到左的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!