android - 如何在 android FragmentPagerAdapter 中添加页面标题和图标

标签 android fragmentpageradapter

我想在标题页标题中显示带有相应图标的标题。如下图,但只能显示标题和缺少的图标。

这是我的示例代码,。

public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

    @Override
    public Fragment getItem(int position) {
        // getItem is called to instantiate the fragment for the given page.
        // Return a DummySectionFragment (defined as a static inner class
        // below) with the page number as its lone argument.

        Log.e("POSITION", "position=" + position);
        Fragment fragment;
        switch (position) {
        case 0:
         //Fragment1
        case 1:
         //Fragment2
        case 2:
         //Fragment3
        case 3:
         //Fragment4
        case 4:
         //Fragment5

        }
        return null;

    }

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

    // using both these techniques but could not get icon in title
    /*
     * public int getIconResId(int index) { return ICONS[index % ICONS.length]; }
     */

    public int getPageIconResId(int position) {
        return ICONS[position];
    }

    @Override
    public CharSequence getPageTitle(int position) {
        Locale l = Locale.getDefault();
        switch (position) {
        case 0:
        return getString(R.string.title_section1).toUpperCase(l);
        case 1:
        return getString(R.string.title_section2).toUpperCase(l);
        case 2:
        return getString(R.string.title_section3).toUpperCase(l);
        case 3:
        return getString(R.string.title_section4).toUpperCase(l);
        case 4:
        return getString(R.string.title_section5).toUpperCase(l);
        }
        return null;
    }

    }

在 onCreate 中:
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.pager);
    mViewPager.setAdapter(mSectionsPagerAdapter);

使用此xml文件在 fragment 中显示 fragment 页面适配器;
<android.support.v4.view.ViewPager
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/rlv2"
        android:layout_marginTop="-30dp" >

        <android.support.v4.view.PagerTitleStrip
            android:id="@+id/pager_title_strip"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:paddingBottom="4dp"
            android:paddingTop="4dp"  
            android:textColor="@color/black"
            android:textStyle="bold" />
    </android.support.v4.view.ViewPager>

我需要我的应用程序的输出图像。请问您知道如何显示带有图标和标题的部分页面适配器。

enter image description here

最佳答案

如果您使用的是设计支持库 TabLayout ,您可以简单地调用:

tabLayout.getTabAt(i).setIcon();

关于android - 如何在 android FragmentPagerAdapter 中添加页面标题和图标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31260384/

相关文章:

android - 在 Android 中绘制半个环

java - 如何在 NavigationView 中设置徽章?

android 搜索 View 光标与工具栏的相同颜色不可见

android - 程序类型已存在 : BuildConfig

android - Android 标准中的可折叠部分

android - 如何动态更改工具栏和标签布局颜色?

android - 如何让后退按钮转到水平滚动中的某个 Fragment?立即退出应用程序

android - 调用 notifyDataSetChanged 后,FragmentPagerAdapter 不会从 0 开始位置

使用 FragmentPagerAdapter 延迟调用的 Android Fragment onPause 事件

android - 根据 Activity fragment 更改 Activity 中的搜索功能