Android PagerTabStrip设置颜色和开始选项卡

时间:2015-04-09 11:59:32

标签: android tabs pagertabstrip

我想将活动标签的文字颜色更改为白色。现在我正在使用选择器,非活动选项卡是灰色的,但活动选项卡不是白色。

XML选择器:     

    <!-- Tab is active  -->
    <item   android:state_checked="true" 
            android:color="@color/tab_color_active" /> 

    <!-- Default -->
    <item   android:color="@color/tab_color_default"  /> 

</selector>

布局部分:

<android.support.v4.view.PagerTabStrip
    android:id="@+id/pager_tab_strip"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="top"
    android:background="#3D3D3D"
    android:paddingBottom="4dp"
    android:paddingTop="4dp"
    android:textColor="@drawable/tab_selector" />

这是我片段的相关代码:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_community, container, false);

    viewPagerAdapter = new ViewPagerAdapter(getChildFragmentManager());
    viewPager = (ViewPager) rootView.findViewById(R.id.pager);
    viewPager.setAdapter(viewPagerAdapter);

    pagerTabStrip = (PagerTabStrip) rootView.findViewById(R.id.pager_tab_strip);
    pagerTabStrip.setDrawFullUnderline(true);
    pagerTabStrip.setTabIndicatorColor(Color.WHITE);


    return rootView;
}

ViewPagerAdapter:

public class ViewPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 4;
    private String tabTitles[] = new String[] {"Tab 1", "Tab 2", "Tab 3", "Tab 4"};

    public ViewPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        switch(position) {
        case 0:
            Fragment1 fragment1 = new Fragment1();
            return fragment1;
        case 1:
            Fragment2 fragment2 = new Fragment2();
            return fragment2;
        case 2:
            Fragment3 fragment3 = new Fragment3();
            return fragment3;
        case 3:
            MyImagesFragment fragment4 = new MyImagesFragment();
            return fragment4;
        }

        return null;
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();

        return tabTitles[position].toUpperCase(l);
    }
}

以下是它的样子: http://fs1.directupload.net/images/150409/3msfxnnl.png

我希望Tab 1的文本颜色为白色,因为它是活动的。此外,我希望起始选项卡是选项卡2而不是选项卡1。 有谁知道怎么做?

0 个答案:

没有答案