我在使用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" />
我缺少的東西還是什麼?任何幫助將不勝感激,謝謝。
您不應該使用'getApplicationContext'。你應該使用'Activity'作爲'Context'。爲什麼你要創建'Runnable'來設置'TabLayout'? – Bryan
是的,即時通訊添加我發佈在代碼中的runnable @Bryan –
*爲什麼*你要添加一個'Runnable'?沒有理由。 – Bryan