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 选项卡向左对齐
基础教程推荐
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- Android:对话框关闭而不调用关闭 2022-01-01