标签标题未显示

时间:2016-11-01 19:27:34

标签: android android-viewpager android-tablayout

我使用TabLayout时遇到了问题。我将设计库更新到v25.0.0,现在标签标题没有显示出来。这是我的代码:

public void setupTabs() {
    animateUserProfileOptions();
    setupViewPager(viewPager);
}

public void setupViewPager(final ViewPager viewPager) {
    TabPagerAdapter adapter = new TabPagerAdapter(getSupportFragmentManager());
    Fragment news = new NotificationNews();
    Fragment request = new NotificationRequests();

    adapter.addFragment(news , getApplicationContext().getString(R.string.notification_news));
    adapter.addFragment(request, getApplicationContext().getString(R.string.notification_request));
    viewPager.setAdapter(adapter);
    tlUserProfileTabs.post(new Runnable() {
        @Override
        public void run() {
            tlUserProfileTabs.setupWithViewPager(viewPager);
        }
    });
}

这是我的适配器:

public class TabPagerAdapter extends FragmentPagerAdapter {

    private final List<Fragment> mFragmentList = new ArrayList<>();
    private final List<String> mFragmentTitleList = new ArrayList<>();

    public TabPagerAdapter(android.support.v4.app.FragmentManager manager) {
        super(manager);
    }

    @Override
    public android.support.v4.app.Fragment getItem(int position) {
        return mFragmentList.get(position);
    }

    @Override
    public int getCount() {
        return mFragmentList.size();
    }

    public void addFragment(Fragment fragment, String title) {
        mFragmentList.add(fragment);
        mFragmentTitleList.add(title);
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mFragmentTitleList.get(position);
    }

}

这是我的xml布局   enter code here    

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/home_primary"
        app:layout_scrollFlags="scroll|enterAlways"
        app:popupTheme="@style/ThemeOverlay.AppCompat.Light" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tlUserProfileTabs"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="#f2f2f2"
        app:tabGravity="fill"
        app:tabIndicatorColor="#28A9E0"
        app:tabTextColor="#999999"
        app:tabSelectedTextColor="#28A9E0"
        app:tabTextAppearance="@drawable/tab_selector"
        app:tabIndicatorHeight="4dp"
        app:tabMode="fixed" />
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />

我错过了什么或什么?任何帮助将不胜感激,谢谢。

2 个答案:

答案 0 :(得分:2)

尝试删除 tabTextAppearance

app:tabTextAppearance="@drawable/tab_selector"

它可能与应用选择的文字颜色和文字颜色相冲突

答案 1 :(得分:0)

TabPagerAdapter应该更像这样:

public class TabPagerAdapter extends FragmentPagerAdapter {

    private final ArrayMap<CharSequence, Fragment> mData = new ArrayMap<>();

    private TabPagerAdapter(FragmentManager manager {
        super(manager);
    }

    public void addFragment(CharSequence title, Fragment fragment) {
        mData.put(title, fragment);
    }

    @Override
    public Fragment getItem(int position) {
        return mData.valueAt(position);
    }

    @Override
    public int getCount() {
        return mData.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return mData.valueAt(position);
    }

}

设置看起来应该更像这样:

public void setupViewPager() {
    TabPagerAdapter adapter = new TabPagerAdapter(getSupportFragmentManager());
    adapter.addFragment(getString(R.string.notification_news, new NotificationNews());
    adapter.addFragment(getString(R.string.notification_request, new NotificationRequest());

    mViewPager.setAdapter(adapter);
    mTabLayout.setupWithViewPager(mViewPager);
}