TabLayout not show icon with android support library 23.2.0

末鹿安然 提交于 2019-12-10 22:17:31

问题


I'v just updated my Android Studio to the latest version. This update comes with the support design library version 23.2.0

I'v used TabLayout with icon in my app (convert Drawable to SpannableString) and it works well with the support design library version 23.1.1 but not version 23.2.0.

Any ideas for this problem?


回答1:


If you are overriding using TabLayout and overriding addTab(Tab), then override

public void addTab(Tab tab, boolean setSelected)

and

public void addTab(Tab tab, int position, boolean setSelected)

From the implementation you can see, that these two methods are not chained, so your code will only get called once, depending on which method the framework uses.

Looks like version 23.2.0 changed from invoking addTab(Tab) to addTab(Tab tab, boolean setSelected) directly




回答2:


here is the answer for icons:

mTabLayout.setupWithViewPager(mViewPager);
// and then:
for (int i = 0; i < tabLayout.getTabCount(); i++) {
     tabLayout.getTabAt(i).setIcon(R.drawable.btn_add_card);
}

found here: Tablayout with icons only




回答3:


I had the same problem after updating to Android Support Library 23.2.0 and later to 23.3.0.

After googling around without finding any answer I finally solved it myself. My solution was to update the icons directly after each time the notifyDataSetChanged() was invoked (and maybe you need it in more places) like below:

mSectionsPagerAdapter.notifyDataSetChanged();
mTabLayout.getTabAt(0).setIcon(R.drawable.tab_0_icon);
mTabLayout.getTabAt(1).setIcon(R.drawable.tab_1_icon);
mTabLayout.getTabAt(2).setIcon(R.drawable.tab_2_icon);
mTabLayout.getTabAt(3).setIcon(R.drawable.tab_3_icon);

The members used above is set in Activity's onCreate() like below:

...

private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
private TabLayout mTabLayout;

...

@Override
protected void onCreate(Bundle savedInstanceState) {

    ...

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

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

    mTabLayout = (TabLayout) findViewById(R.id.tabs);
    mTabLayout.setupWithViewPager(mViewPager);

    mTabLayout.getTabAt(0).setIcon(R.drawable.tab_0_icon).select();
    mTabLayout.getTabAt(1).setIcon(R.drawable.tab_1_icon);
    mTabLayout.getTabAt(2).setIcon(R.drawable.tab_2_icon);
    mTabLayout.getTabAt(3).setIcon(R.drawable.tab_3_icon);

    ...
}


来源:https://stackoverflow.com/questions/35625373/tablayout-not-show-icon-with-android-support-library-23-2-0

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!