Android tablayout中的选项卡未显示选项卡图标

Android tablayout中的选项卡未显示选项卡图标,android,android-fragments,android-viewpager,android-tablayout,Android,Android Fragments,Android Viewpager,Android Tablayout,我已经创建了tablayout。并在每个选项卡的左侧添加图标。但是我看不到那些图标。 下面是java文件的代码 public class ActivityAllVerification extends AppCompatActivity { SessionManager sessionManager; Toolbar toolbar; private TabLayout tabLayout; private ViewPager viewPager; @

我已经创建了tablayout。并在每个选项卡的左侧添加图标。但是我看不到那些图标。 下面是java文件的代码

public class ActivityAllVerification extends AppCompatActivity {

    SessionManager sessionManager;
    Toolbar toolbar;
    private TabLayout tabLayout;
    private ViewPager viewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_all_verification);
        initDefaultSettings();

    }

    private void initDefaultSettings() {
        sessionManager = new SessionManager(ActivityAllVerification.this);
        toolbar = (Toolbar) findViewById(R.id.appbar);
        setSupportActionBar(toolbar);
        ((ImageView) toolbar.findViewById(R.id.bitcoin_logo)).setVisibility(View.GONE);
        TextView txt = (TextView) toolbar.findViewById(R.id.toolbar_txt_title);
        txt.setText("Account Verification");
        getSupportActionBar().setDisplayShowHomeEnabled(true);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setDisplayShowTitleEnabled(false);//disable default action bar title
        // Toast.makeText(this, "" + sessionManager.isUser(), Toast.LENGTH_SHORT).show();
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();
        tabLayout.post(new Runnable() {
            @Override
            public void run() {
                tabLayout.setupWithViewPager(viewPager);
            }
        });

    }

    private void setupTabIcons() {
        TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabOne.setText("PAN CARD");
        tabOne.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_bitcoin_24, 0, 0, 0);
        tabLayout.getTabAt(0).setCustomView(tabOne);

        TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabTwo.setText("AADHAR CARD");
        tabOne.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_bitcoin_24, 0, 0, 0);
        tabLayout.getTabAt(1).setCustomView(tabTwo);

        TextView tabThree = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
        tabThree.setText("BACK ACCOUNT");
        tabOne.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_bitcoin_24, 0, 0, 0);
        tabLayout.getTabAt(2).setCustomView(tabThree);
        Toast.makeText(this, "" + sessionManager.isUser(), Toast.LENGTH_SHORT).show();
        if (!sessionManager.isUser())
        {
            TextView tabFour = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
            tabThree.setText("TAX INCOICE");
            tabOne.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_bitcoin_24, 0, 0, 0);
            tabLayout.getTabAt(3).setCustomView(tabFour);
        }
    }

    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new FragmentPanCard(), "PAN CARD");
        adapter.addFragment(new FragmentAadharCard(), "AADAHR CARD");
        adapter.addFragment(new FragmentBankAccount(), "BANK ACCOUNT");
        if (!sessionManager.isUser())
            adapter.addFragment(new FragmentTaxInvoice(), "TAX INVOICE");
        viewPager.setAdapter(adapter);
    }



    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = new ArrayList<>();
        private final List<String> mFragmentTitleList = new ArrayList<>();

        public ViewPagerAdapter(FragmentManager manager) {
            super(manager);
        }

        @Override
        public 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);
        }
    }
}

因此,现在它的显示选项卡文本颜色为黑色。我希望选项卡文本颜色为白色,带有左侧图标。如何实现这一点?

我曾为同样的问题挣扎过一段时间,直到我发现,如果在设置图标后调用SectionsPageAdapter的notifyDataSetChanged,选项卡图标将不会显示

只要确保每次调用notifyDataSetChanged()后都设置图标


您已经在
setupViewPager()
方法中为选项卡提供了标题。删除
setupTabIcons()
方法中的给定标题,并尝试仅使用
tabLayout.getTabAt(0).setIcon(R.drawable.icon)内部
setupTabIcons()
尝试删除此行tabLayout.post(新Runnable(){@Override public void run(){tabLayout.setupWithViewPager(viewPager);})@感谢您的回复。但它给予的是同样的output@Adilhusen. 谢谢你的回复。但现在它不显示任何选项卡的标题,只显示第一个选项卡的图标
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="4dp"
    android:id="@+id/tab"
    android:textColor="@color/colorWhite"
    android:fontFamily="@string/font_fontFamily_medium"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/background_color">

        <include
            android:id="@+id/appbar"
            layout="@layout/toolbar"></include>

        <android.support.design.widget.TabLayout
            android:id="@+id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@+id/appbar"
            app:tabGravity="fill"
            app:tabMode="scrollable"
            app:tabIndicatorColor="@color/colorWhite"
            app:tabTextColor="@color/colorWhite"/>


        <android.support.v4.view.ViewPager
            android:id="@+id/viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/tabs"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />
    </RelativeLayout>
tabLayout.post(new Runnable() {
        @Override
        public void run() {
            tabLayout.setupWithViewPager(viewPager);
        }
    });
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
TabLayout tabLayout;

private int[] tabIcons = {
    R.drawable.icon_tab_settings,
    R.drawable.icon_tab_edit,
    R.drawable.icon_tab_recordings,
    R.drawable.icon_tab_pentagram
};

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ...

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
    mViewPager = findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);
    mViewPager.setCurrentItem(1);
    tabLayout = findViewById(R.id.tabs);
    tabLayout.setupWithViewPager(mViewPager);

    tabLayout.getTabAt(0).setIcon(tabIcons[0]);
    tabLayout.getTabAt(1).setIcon(tabIcons[1]);
    tabLayout.getTabAt(2).setIcon(tabIcons[2]);
    tabLayout.getTabAt(3).setIcon(tabIcons[3]);


    ...
}

public void refreshView(){
    if(mSectionsPagerAdapter!=null){
        mSectionsPagerAdapter.notifyDataSetChanged();

        //refresh tab icons again
        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
        tabLayout.getTabAt(2).setIcon(tabIcons[2]);
        tabLayout.getTabAt(3).setIcon(tabIcons[3]);
    } 
}