繁体   English   中英

从viewpager适配器设置页面标题后,页面标题未出现在ViewPager中

[英]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>

它是我的代码,您可以根据需要自定义它。

PagerTabStrip被Google忽略了很多。 您不妨考虑切换到其他ViewPager选项卡指示器。

如果您的compileSdkVersion为23,请确保您使用的是最新版本的v23 Android支持库。 现在是23.4.0 因此,例如,您可能依赖于support-v4:23.4.0 您看到的问题已解决

如果您的compileSdkVersion为24,请密切注意此问题 ,因为似乎有一个新错误。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM