[英]Page title does not appear in ViewPager after setting it from the viewpager adapter
从适配器设置标题后,标题未出现在PagerTabStrip中
这是我的活动
public class MainActivity extends AppCompatActivity {
private ViewPager pager ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pager = (ViewPager) findViewById(R.id.pager);
pager.setAdapter(new PagerAdapter(getSupportFragmentManager(),this));
}
}
这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.PagerTabStrip
android:id="@+id/titlepage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
/>
</android.support.v4.view.ViewPager>
这是适配器
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm , Context ctxt) {
super(fm);
}
@Override
public Fragment getItem(int position) {
return MyFragment.newInstance(position);
}
@Override
public int getCount() {
return 10;
}
@Override
public String getPageTitle(int position) {
return "title" + position ;
}
这是应用程序中使用的主题:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
</style>
结果 :-
如您在上图中看到的,即使我在getPageTitle方法中设置了标题,viewpager中也没有标题出现,有人可以告诉我是什么错误导致我无法在viewpager中添加标题?
在您的布局中使用它
<android.support.design.widget.AppBarLayout
android:id="@+id/Parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="60dp">
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:theme="@style/ThemeOverlay.AppCompat.ActionBar" />
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.design.widget.AppBarLayout>
它是我的代码,您可以根据需要自定义它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.