TabLayout两种带图标的Tab方法

第一种:TabLayout+ViewPager

主要通过tab.setCustomView()设置图片的

public class SimpleFragmentPagerAdapter extends FragmentViewPageAdapter{

    private ListmTitle;
    private List mList;
    private Context mContext;
    public SimpleFragmentPagerAdapter(FragmentManager fm, List mList, List mTitle,Context mContext) {
        super(fm, mList, mTitle);
        this.mContext=mContext;
        this.mList=mList;
        this.mTitle=mTitle;
    }


    @Override
    public Fragment getItem(int position) {
        return mList.get(position);
    }

    @Override
    public int getCount() {
        return mTitle.size();
    }


    /**
     * 获取标题
     * @param position
     * @return
     */
    @Override
    public CharSequence getPageTitle(int position) {
        SpannableString sb = new SpannableString("   " +       mTitle.get(position));
        if (position==2){
            Drawable image = mContext.getResources().getDrawable(R.mipmap.select);
            image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
            ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
            sb.setSpan(imageSpan, 0, 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
            return sb;
        }
        else {
            return mTitle.get(position);
        }
        // Replace blank spaces with image icon

    }

    /**
     * 也可以写到Activity中  通过tab  setCustomView  改变选项卡
     * @param position
     * @return
     */
    public View getTabView(int position){
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_pager, null);
        TextView tv= (TextView) view.findViewById(R.id.textView);
        tv.setText(mTitle.get(position));
        ImageView img = (ImageView) view.findViewById(R.id.imageView);
        if (position==2){
            img.setImageResource(R.mipmap.select);
        }

        return view;
    }

在Activity中

 mListFragment=new ArrayList<>();
//        mTitles.add("热度");
        mTitles.add("销量");
        mTitles.add("价格");
        mTitles.add("优惠");
//        mListFragment.add(ValidFragment.getIntance("数据"));
        mListFragment.add(new InvalidFragment());
        mListFragment.add(ValidFragment.getIntance("数据"));
        mListFragment.add(new InvalidFragment());

        adapter=new   SimpleFragmentPagerAdapter(getSupportFragmentManager(),mListFragment,mTitles,this);
        wallet_viewpager.setAdapter(adapter);
        mTablayout.setupWithViewPager(wallet_viewpager);
        mTablayout.setTabGravity(TabLayout.GRAVITY_FILL);
         //设置
        for (int i = 0; i < mTablayout.getTabCount(); i++) {
            if (i==2){
                TabLayout.Tab tab = mTablayout.getTabAt(i);
                tab.setCustomView(adapter.getTabView(i));
            }

        }

谢谢泡在网上的日子

第二种方法Tablayout+LinearLayout

mTitles = new ArrayList<>();
        mListFragment = new ArrayList<>();
        mTitles.add("热度");
        mTitles.add("销量");
        mTitles.add("价格");
        mTitles.add("优惠");
        /**
         * 通过标题的数量动态添加Tab
         */
        for (int i = 0; i < mTitles.size(); i++) {
            TabLayout.Tab tab = mTablayout.newTab();
            View view = LayoutInflater.from(this).inflate(R.layout.item_pager, null);
            TextView tv = (TextView) view.findViewById(R.id.textView);
            tv.setText(mTitles.get(i));
            ImageView img = (ImageView) view.findViewById(R.id.imageView);
            /**
             * 自定义一个布局
             */
            tab.setCustomView(view);
            if (i == 2) {
                img.setVisibility(View.VISIBLE);
                img.setBackgroundResource(R.mipmap.select);
                tv.setFocusable(true);
            }
            mTablayout.addTab(tab);

        }

选中的代码

mTablayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mFragmentManager = getSupportFragmentManager();
                mFragmentTransction = mFragmentManager.beginTransaction();
                switch (tab.getPosition()) {

                    case 0:
                        if (InvalidFragment == null) {
                            InvalidFragment = new InvalidFragment();
                        }
                        mFragmentTransction.replace(R.id.tb, InvalidFragment);
                        break;
                    case 1:
                        if (validFragment == null) {
                            validFragment = ValidFragment.getIntance("数据");
                        }
                        mFragmentTransction.replace(R.id.tb, validFragment);
                        break;
                    case 2:
                        if (validFragment == null) {
                            validFragment = ValidFragment.getIntance("数据");
                        }
                        mFragmentTransction.replace(R.id.tb, validFragment);
                        break;
                    case 3:
                        if (InvalidFragment == null) {
                            InvalidFragment = new InvalidFragment();
                        }
                        mFragmentTransction.replace(R.id.tb, InvalidFragment);
                        break;


                }
                mFragmentTransction.commit();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

tab 布局


<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_centerInParent="true"
    android:layout_centerHorizontal="true"
    >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:gravity="center"
        android:layout_centerInParent="true"
        android:textColor="@color/white"
        android:layout_marginLeft="3dp"
        android:text="fffffff"
        />
    <ImageView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/imageView"
        android:visibility="gone"
        android:layout_toRightOf="@+id/textView"
        android:layout_centerVertical="true"

        />

RelativeLayout>

你可能感兴趣的:(android)