PagerTabStrip tabs alignment to left(PagerTabStrip 选项卡向左对齐)
问题描述
我正在使用 PagerTabStrip 设置滑动标签.但是,当我设置三个选项卡时,它会在中心显示第一个选项卡,因此用户第一眼看不到所有三个选项卡.
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.
如何将它们左对齐?
我想像这样将标签向左移动.
I want to move tabs to the left like this.
我尝试了重力,但没有运气.
I tried gravity, but no luck.
pagerTabStrip.setGravity(Gravity.LEFT);
这是 MainActivity 的代码.
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);
}
和
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 下载最新的代码,而不是从谷歌开发者页面.
The Problem Solved!!
You need to download the latest codes from github instead of google developer page.
SlidingTabLayout.java
SlidingTabStrip
然后,您可以像这样使用 setDistributeEvenly(true).
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);
}
推荐答案
在setViewPager()之前使用这个:
Use this before setViewPager():
sliding_tabs.setDistributeEvenly(true);
如果您收到此错误 Cannot resolve method 'setDistributeEvenly(boolean)'
,您应该从 Google I/O 源代码更新您的 SlidingTabLayout.java 和 SlidingTabStrip.java:
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
这篇关于PagerTabStrip 选项卡向左对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:PagerTabStrip 选项卡向左对齐
- 网上有没有好的 UIScrollView 教程? 2022-01-01
- 如何在 iPhone 模拟器中重置 NSUserDefaults 数据? 2022-01-01
- Xcode 7.3 中带有 UILabel 的 UIStackView 2022-01-01
- URL编码Swift iOS 2022-01-01
- 在 Iphone SDK 的导航栏上添加多个按钮 2022-01-01
- UITextView 内容插图 2022-01-01
- GPS状态的广播接收器? 2022-01-01
- 类似于 Mail.app 的 iPad 模态视图控制器? 2022-01-01
- 使用自动布局向 UIScrollView 添加动态大小的视图 2022-01-01
- SetOnItemSelectedListener上的微调程序错误 2022-01-01