Tablayout with icons only

后端 未结 12 1381
暗喜
暗喜 2020-12-04 11:32

I am using design support to create tabs. I am also using ViewPager for swipable tabs.

Now, I don\'t know how to use only icons instead of texts in tabs

相关标签:
12条回答
  • 2020-12-04 11:46

    In TabLayout, setting icon is easy:

    getPageTitle(position) should return null (if you don't want title to show).

    Next :

    tabLayout.getTabAt(0).setIcon(resId);
    
    tabLayout.getTabAt(1).setIcon(resId);
    
    ......
    
    0 讨论(0)
  • 2020-12-04 11:54

    The tutorial shown in the following link should cover what you want. https://github.com/codepath/android_guides/wiki/Google-Play-Style-Tabs-using-TabLayout#add-icons-to-tablayout

    I copied the relevant section below.

    Add Icons to TabLayout

    Currently, the TabLayout class does not provide a clean abstraction model that allows for icons in your tab. There are many posted workarounds, one of which is to return a SpannableString, containing your icon in an ImageSpan, from your PagerAdapter's getPageTitle(position) method as shown in the code snippet below:

    private int[] imageResId = {
            R.drawable.ic_one,
            R.drawable.ic_two,
            R.drawable.ic_three
    };
    
    // ...
    
    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        // return tabTitles[position];
        Drawable image = context.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;
    }
    

    By default, the tab created by TabLayout sets the textAllCaps property to be true, which prevents ImageSpans from being rendered. You can override this behavior by changing the tabTextAppearance property.

      <style name="MyCustomTabLayout" parent="Widget.Design.TabLayout">
            <item name="tabTextAppearance">@style/MyCustomTextAppearance</item>
      </style>
    
      <style name="MyCustomTextAppearance" parent="TextAppearance.Design.Tab">
            <item name="textAllCaps">false</item>
      </style>
    
    0 讨论(0)
  • 2020-12-04 11:55

    Try this this will definitely work .

    private TabLayout tabLayout;
    private ViewPager viewPager;
    private int[] tabIcons = {
            R.drawable.single,
            R.drawable.multiple};
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_contact_picker);
    
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        toolbar.setTitle("Choose contact");
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        tab();
    }
    
    
    public void tab(){
        viewPager = (ViewPager) findViewById(R.id.viewpager);
        setupViewPager(viewPager);
        tabLayout = (TabLayout) findViewById(R.id.tabs);
        tabLayout.setupWithViewPager(viewPager);
        setupTabIcons();
    }
    
    private void setupTabIcons() {
        tabLayout.getTabAt(0).setIcon(tabIcons[0]);
        tabLayout.getTabAt(1).setIcon(tabIcons[1]);
    
    }
    
    private void setupViewPager(ViewPager viewPager) {
        ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(new Login());
        adapter.addFragment(new Register());
        viewPager.setAdapter(adapter);
    }
    
    class ViewPagerAdapter extends FragmentPagerAdapter {
        private final List<Fragment> mFragmentList = 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) {
            mFragmentList.add(fragment);
    
        }
    
    
    }
    
    0 讨论(0)
  • 2020-12-04 11:58

    try this

        public class GlobalActivity extends AppCompatActivity {
        Toolbar toolbar;
        ViewPager viewPager;
        TabLayout tabLayout;
        ViewPagerAdapter adapter;
        private int[] tabIcons = {
                R.drawable.home_ic,
                R.drawable.biz_ic,
                R.drawable.network_ic,
                R.drawable.offers_ic,
                R.drawable.message_ic_b
        };
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_global_hub);
            tab();
        }
        public void tab(){
            viewPager = (ViewPager) findViewById(R.id.viewpager);
            setupViewPager(viewPager);
            tabLayout = (TabLayout) findViewById(R.id.tablayout);
            tabLayout.setupWithViewPager(viewPager);
            setupTabIcons();
    
        }
        private void setupTabIcons() {
            tabLayout.getTabAt(0).setIcon(tabIcons[0]);
            tabLayout.getTabAt(1).setIcon(tabIcons[1]);
            tabLayout.getTabAt(2).setIcon(tabIcons[2]);
            tabLayout.getTabAt(3).setIcon(tabIcons[3]);
            tabLayout.getTabAt(4).setIcon(tabIcons[4]);
    
        }
        public void setupViewPager(ViewPager viewPager){
            adapter = new ViewPagerAdapter(getSupportFragmentManager());
            adapter.addFrag(new GlHubFragment(),"HOME");
            adapter.addFrag(new BizForumFragment(), "BIZ FORUM");
            adapter.addFrag(new NetworkFragment(), "NETWORK");
            adapter.addFrag(new MessagesFragment(), "MESSAGEs");
            adapter.addFrag(new OfferFragmentActivity(), "OFFER");
            viewPager.setAdapter(adapter);
        }
    
        public class ViewPagerAdapter extends FragmentPagerAdapter{
            private final List<Fragment> mfragmentlist =new ArrayList<>();
            private final List<String> mFragmentTitleList = new ArrayList<>();
            public ViewPagerAdapter(FragmentManager fm) {
                super(fm);
            }
    
            @Override
            public Fragment getItem(int position) {
                return mfragmentlist.get(position);
            }
    
            @Override
            public int getCount() {
                return mfragmentlist.size();
            }
            public void addFrag(Fragment fragment,String title){
                mfragmentlist.add(fragment);
                mFragmentTitleList.add(title);
            }
            @Override
            public CharSequence getPageTitle(int position){
                return mFragmentTitleList.get(position);
            }
        }
    }
    
    0 讨论(0)
  • 2020-12-04 11:59

    None of these methods are elegant when using TabLayout as the ViewPager "decor" scenario. TabLayout Documentation Here is a simple extension of TabLayout and PagerAdapter that provides a simple drop in replacement for TabLayout that should be able to be used in either scenario without manually adding icons outside of the TabLayout class and following the usage of PagerAdapter to get the tab information.

    /**
     * Created by JDL on 1/18/2017.
     */
    public class TabLayoutExt extends TabLayout {
    
        protected ViewPager mViewPager;
    
        public abstract static class TabLayoutViewPagerAdapter extends PagerAdapter {
            public TabLayoutViewPagerAdapter() {
            }
    
            /**
             * This method may be called by the TabLayout to obtain an icon drawable
             * to for the specified tab. This method may return null
             * indicating no tab icon for this page. The default implementation returns
             * null.
             *
             * @param position The position of the title requested
             * @return A drawable icon for the requested page
             */
            public Drawable getPageIcon(Context context, int position) {
                return null;
            }
        }
    
        public TabLayoutExt(Context context) {
            super(context);
        }
    
        public TabLayoutExt(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        public TabLayoutExt(Context context, AttributeSet attrs, int defStyleAttr) {
            super(context, attrs, defStyleAttr);
        }
    
        @Override
        protected void onAttachedToWindow() {
            //Cover the implicit setup in TabLayout
            if (mViewPager == null) {
                final ViewParent vp = getParent();
                if (vp instanceof ViewPager) {
                    mViewPager = (ViewPager)vp;
                }
    
            }
            super.onAttachedToWindow();
        }
    
        public void addTab(@NonNull Tab tab, int position, boolean setSelected) {
            if (mViewPager != null && mViewPager.getAdapter() instanceof TabLayoutViewPagerAdapter) {
                Drawable icon = ((TabLayoutViewPagerAdapter) mViewPager.getAdapter()).getPageIcon(getContext(),position);
                tab.setIcon(icon);
            }
            super.addTab(tab,position,setSelected);
    
        }
    
        @Override
        public void setupWithViewPager(@Nullable ViewPager viewPager, boolean autoRefresh) {
            mViewPager = viewPager;
            super.setupWithViewPager(viewPager, autoRefresh);
        }
    }
    

    So all that needs be done is extend TabLayoutViewPagerAdapter instead of PageAdapter and implement getPageIcon(Context,int) instead of or in addition to title. The drop in TabLayoutExt in your XML file, instead of the normal TabLayout. This could be extended for further modifying the tab, either with a custom view instead or something else.

    0 讨论(0)
  • 2020-12-04 11:59

    the simplest way is create new table by setting Icon on tablayout. below code will create two tab with icon only. use this code on onCreate() method

     tablayout = (TabLayout) findViewById(R.id.order_tablayout);
     tablayout.addTab( tablayout.newTab().setIcon(getResources().getDrawable(R.mipmap.ic_shopping_cart_white_24dp)) );
     tablayout.addTab( tablayout.newTab().setIcon(getResources().getDrawable(R.mipmap.ic_like2_fille_white_24dp)) );
    
    0 讨论(0)
提交回复
热议问题