PagerTabStrip 选项卡向左对齐

PagerTabStrip tabs alignment to left(PagerTabStrip 选项卡向左对齐)
本文介绍了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 选项卡向左对齐的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

How to target newer versions in .gitlab-ci.yml using auto devops (java 11 instead of 8 and Android 31 instead of 29)(如何在.gitlab-ci.yml中使用自动开发工具(Java 11而不是8,Android 31而不是29)瞄准较新的版本)
Android + coreLibraryDesugaring: which Java 11 APIs can I expect to work?(Android+core LibraryDesugering:我可以期待哪些Java 11API能够工作?)
How to render something in an if statement React Native(如何在If语句中呈现某些内容Reaction Native)
How can I sync two flatList scroll position in react native(如何在本机Reaction中同步两个平面列表滚动位置)
Using Firebase Firestore in offline only mode(在仅脱机模式下使用Firebase FiRestore)
Crash on Google Play Pre-Launch Report: java.lang.NoSuchMethodError(Google Play发布前崩溃报告:java.lang.NoSuchMethodError)