How can i develop the PagerSlidingTabStrip with images in android?

后端 未结 3 1974
南笙
南笙 2020-12-06 14:47

I am using the PagerSlidingTabStrip in myapp .In this titles are setting fine but images are not setting tabstrip .I searched alot in google but didn\'t get the correct resu

3条回答
  •  不知归路
    2020-12-06 15:35

    I was also interested doing same as yours, Finally I did it for you.

    Import 'SlidingTabsBasic' demo from android-sdk sample or download it from google android samples from Here.

    1.create custom_tab.xml with only a TextView 2.In 'SlidingTabsBasicFragment.class' do below changes:

    -add this code in onViewCreated()

    mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.sliding_tabs);
    mSlidingTabLayout.setCustomTabView(R.layout.custom_tab, 0);
    mSlidingTabLayout.setViewPager(mViewPager);
    

    -in SamplePagerAdapter class, replace this function code

    public CharSequence getPageTitle(int position)
    {
        //return "Item " + (position + 1);
        Drawable image = getActivity().getResources().getDrawable(imageResId[position]);
        image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
        SpannableString sb = new SpannableString(" ");
        ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
        sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return sb;
    }
    

    If you have done it properly, You will get something like this:

    Tab Strip

    Hope this will help!

提交回复
热议问题