如何使两个类别的Pager Sliding Tab Strip同样适合屏幕android?

时间:2014-12-05 06:28:53

标签: android android-viewpager android-pageradapter

我正在使用寻呼机滑动标签条,我在右侧获得额外的空间。如何在选项卡中同样适合两个分类?

我的代码如下:

    PagerSlidingTabStrip tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs);
    tabs.setIndicatorColor(Color.BLUE);
    tabs.setIndicatorHeight(2);
    mPager = (ViewPager) findViewById(R.id.pager);
    FragmentManager fm = getSupportFragmentManager();
    ViewPagerAdapter adapter = new ViewPagerAdapter(fm);
    mPager.setAdapter(adapter);
    tabs.setViewPager(mPager);
    tabs.setShouldExpand(true);

        public class ViewPagerAdapter extends FragmentPagerAdapter {

    // Declare the number of ViewPager pages
    //final int PAGE_COUNT = 2;

    public ViewPagerAdapter(android.support.v4.app.FragmentManager fm) {
        super(fm);
    }
    private final String[] TITLES = { "SIGN IN", "REGISTER"};

    @Override
    public CharSequence getPageTitle(int position) {
        return TITLES[position];
    }

我的xml是:

  <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<com.views.PagerSlidingTabStrip
    android:id="@+id/tabs"
    android:layout_width="match_parent"
    android:layout_height="48dip"
    android:background="@drawable/background_tabs" />

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:background="#000000"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/colors"
    android:layout_below="@+id/tabs"
    tools:context=".MainActivity" />

这是我的截图

enter image description here

5 个答案:

答案 0 :(得分:7)

我认为不必在java文件中添加setShouldExpand,而是将其添加到xml文件中......就像这样..

<com.views.PagerSlidingTabStrip
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
android:background="@drawable/background_tabs" 
app1:pstsShouldExpand="true"/>

还在您的父布局中添加xmlns:app1="http://schemas.android.com/apk/res/your.package.name",如下所示..

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app1="http://schemas.android.com/apk/res/your.package.name" >

希望它有效...... !!

答案 1 :(得分:2)

添加

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lp.weight = 1;
tabView.setLayoutParams(lp);

到SlidingTabLayout中的populateTabStrip()方法。它应该看起来像:

private void populateTabStrip() {
        final PagerAdapter adapter = mViewPager.getAdapter();
        final OnClickListener tabClickListener = new TabClickListener();

        for (int i = 0; i < adapter.getCount(); i++) {
            View tabView = null;
            TextView tabTitleView = null;

            if (mTabViewLayoutId != 0) {
                // If there is a custom tab view layout id set, try and inflate it
                tabView = LayoutInflater.from(getContext()).inflate(mTabViewLayoutId, mTabStrip,
                        false);
                tabTitleView = (TextView) tabView.findViewById(mTabViewTextViewId);
            }

            if (tabView == null) {
                tabView = createDefaultTabView(getContext());
            }

            if (tabTitleView == null && TextView.class.isInstance(tabView)) {
                tabTitleView = (TextView) tabView;
            }

            tabTitleView.setText(adapter.getPageTitle(i));
            tabView.setOnClickListener(tabClickListener);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            lp.weight = 1;
            tabView.setLayoutParams(lp);
            mTabStrip.addView(tabView);
        }
    }

答案 2 :(得分:1)

要像WhatsApp一样分割标签,请在mainActivity.java文件中添加以下代码。

tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

<强> MainActivity.java

    public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Set the content of the activity to use the activity_main.xml layout file
        setContentView(R.layout.activity_main);

        ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);

        // CategoryFragmentPagerAdapter adapter =
        //     new CategoryFragmentPagerAdapter(getSupportFragmentManager());

        viewPager.setAdapter(new CategoryFragmentPagerAdapter(getSupportFragmentManager(),
                MainActivity.this));

        TabLayout tabLayout = (TabLayout) findViewById(R.id.sliding_tabs);
        tabLayout.setupWithViewPager(viewPager);

        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
    }
}
  

请注意让它无法添加   XML文件中的app:tabMode="scrollable"代码。

答案 3 :(得分:0)

在我的情况下,我使用了一些自定义滑动寻呼机,下面的代码行帮助了我,

 View tabView = null;
    if (mTabViewLayoutId != 0) {
        tabView = LayoutInflater.from(getContext()).inflate(
                            mTabViewLayoutId, mTabStrip, false);
        TextView tabTitleView = (TextView) tabView
                            .findViewById(mTabViewTextViewId);
 tabTitleView.setWidth(getResources().getDisplayMetrics().widthPixels
                                    / mNumberOfTabs);
                }

此处,mTabViewLayoutId是自定义布局资源ID

希望这样的事能对你有所帮助。

答案 4 :(得分:0)

您需要通过将宽度设为0来自定义PageSlidingTabStrip。我将显示我在该课程中所做的更改。

public static int width = 0;

然后在MainActivity中:

    Display display = getWindowManager().getDefaultDisplay();
    Point size = new Point();
    display.getSize(size);
    int width = size.x;
    PagerSlidingTabStrip.width = width / 2;
    setContentView(R.layout.loginpager);
    //others are all same.
   PagerSlidingTabStrip tabs = (PagerSlidingTabStrip)findViewById(R.id.tabs);
    tabs.setIndicatorColor(getResources().getColor(R.color.blue));
    tabs.setIndicatorHeight(5);
    tabs.setTextColor(getResources().getColor(R.color.blue));
    FragmentManager fm = getSupportFragmentManager();
    ViewPagerAdapter adapter = new ViewPagerAdapter(fm);
    mPager.setAdapter(adapter);
    tabs.setViewPager(mPager);

这是输出屏幕

enter image description here