使用SlidingTabLayout的自定义标签?

时间:2015-07-09 18:28:11

标签: android android-custom-view android-tabs pagerslidingtabstrip

This is what I have so far This is what I want to achieve

这是我的代码

public class ViewPagerAdapter extends FragmentStatePagerAdapter {

private static int[] ICONS = new int[] {
        R.drawable.image_back_icon,
        R.drawable.image_category_icon,
        R.drawable.activated,
        R.drawable.image_cart_icon,
        R.drawable.image_profile_icon
};

// Build a Constructor and assign the passed Values to appropriate values in the class
public ViewPagerAdapter(FragmentManager fm,CharSequence mTitles[], int mNumbOfTabsumb) {
    super(fm);

}

//This method return the fragment for the every position in the View Pager
@Override
public Fragment getItem(int position) {

    switch (position) {
        case 0: Tab1 tab1 = new Tab1();
                return tab1;
        case 1: Tab2 tab2 = new Tab2();
            return tab2;
        case 2: Tab3 tab3 = new Tab3();
            return tab3;
        case 3: Tab4 tab4 = new Tab4();
            return tab4;
        case 4: Tab5 tab5 = new Tab5();
            return tab5;
        default: return null;
    }
}

// This method return the titles for the Tabs in the Tab Strip

@Override
public CharSequence getPageTitle(int position) {
    return null;
}


// This method return the Number of tabs for the tabs Strip

@Override
public int getCount() {
    return ICONS.length;
}

public int getDrawableId(int position) {
    return ICONS[position];
}
}

public class MainActivity extends AppCompatActivity {

//Toolbar toolbar;
ViewPager pager;
ViewPagerAdapter adapter;
SlidingTabLayout tabs;


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

    setContentView(R.layout.activity_main);

    adapter =  new ViewPagerAdapter(getSupportFragmentManager(),Titles,Numboftabs);

    pager = (ViewPager) findViewById(R.id.pager);
    pager.setAdapter(adapter);

    tabs = (SlidingTabLayout) findViewById(R.id.tabs);
    tabs.setDistributeEvenly(true); 
    tabs.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
       @Override
       public int getIndicatorColor(int position) {
            return getResources().getColor(R.color.primary);
        }
    });

    // Setting the ViewPager For the SlidingTabsLayout
    tabs.setViewPager(pager);



}

}

我已达到图1,我想达到图2所示的效果

我希望他们能够做出反应,但效果如图2所示。

我尝试过使用自定义绘图,但它似乎没有用。如何让图标更改颜色并使搜索图标具有背景颜色?

我应该如何实现这一点,使其适用于所有设备和屏幕尺寸。

谢谢。

1 个答案:

答案 0 :(得分:0)

也许你忘了打电话了

slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setViewPager(viewPager);