Like putting icon and text in tabs (Sliding Tabs) android?

后端 未结 3 1239
情深已故
情深已故 2021-01-29 01:10

I need to make my application with this appearance (icon and text): https://material-design.storage.googleapis.com/publish/material_v_4/material_ext_publish/0B6Okdz75tqQsbHJuWi0

3条回答
  •  星月不相逢
    2021-01-29 01:45

    You can achieve your task with the following very easily. Firstly create new XML under Layout Folder. Give it name [custom_tab_view] then paste the below code.

    
    
    
        
    
            
    
            
    
    
    
    

    And now went to your ViewPageAdapter and Replace your Function [CharSequence] with below code.

     // This method return the titles for the Tabs in the Tab Strip
        @Override
        public CharSequence getPageTitle(int position) {
            //return Titles[position];
            Drawable image = mContext.getResources().getDrawable(imageResId[position]);
            image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
            //image.setBounds(0, 0, 64, 64);
            ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
            SpannableString sb = new SpannableString(" ");
            sb.setSpan(imageSpan, 0, sb.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
    
    
            return sb;
        }
    

    and in the MainActivity under OnCreate put this line if it is not already there.

    SlidingTabLayout tabs;
    tabs.setCustomTabView(R.layout.custom_tab_view, R.id.tabText);
    
    
                                                     Hope it answer the Question.
    

提交回复
热议问题