Android TabLayout 添加图片图标 下滑线和文字长度一样

Android TabLayout 添加图片图标 下滑线和文字长度一样_第1张图片需求是tablayout 上有图片和下滑线和字体长度一样 。tablayout 可以添加布局的 直接上代码 

// tab 适配器
class MyFragmentPagerAdapter extends FragmentPagerAdapter {
    private MyFragmentPagerAdapter(FragmentManager fm) {
        super(fm);
    }

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

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

    @Override
    public CharSequence getPageTitle(int position) {
        return mTabName.get(position);
    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    public View getTabItemView(int position) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.item_group_tab_layout, null);
        TextView textView = (TextView) view.findViewById(R.id.tv_tab_name);
        View vline = (View) view.findViewById(R.id.v_line);
        ImageView iv = (ImageView) view.findViewById(R.id.iv_tab_icon);
        if (position == 0) {
            iv.setImageResource(R.drawable.rm);
        } else {
            ImageLoader.loadImage(mRequestManager, iv, mListUrlImage.get(position), R.drawable.logo);
        }
        vline.setBackgroundColor(ContextCompat.getColor(mContext, R.color.white));
        textView.setTextColor(ContextCompat.getColor(mContext, R.color.color_666666));
        textView.setTextSize(15);
        textView.setText(mTabName.get(position));
        return view;
    }

}

 

item_group_tab_layout 布局

把view 添加到tablayout中 初始化时候就可以添加 
private void setUpTabBadge(MyFragmentPagerAdapter mPagerAdapter) {
    //
    for (int i = 0; i < mFragments.size(); i++) {
        TabLayout.Tab tab = layoutTab.getTabAt(i);
        // 更新Badge前,先remove原来的customView,否则Badge无法更新
        View customView = tab.getCustomView();
        if (customView != null) {
            ViewParent parent = customView.getParent();
            if (parent != null) {
                ((ViewGroup) parent).removeView(customView);
            }
        }
        // 更新CustomView
        tab.setCustomView(mPagerAdapter.getTabItemView(i));
    }

}
layoutTab 选择监听改变 下划线 字体颜色 即可    setUpTabBadge(mPagerAdapter)重新初始化talayout里面布局
layoutTab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
    @Override
    public void onTabSelected(TabLayout.Tab tab) {
        setUpTabBadge(mPagerAdapter);
        View vline = (View) tab.getCustomView().findViewById(R.id.v_line);
        TextView tv_tabName = tab.getCustomView().findViewById(R.id.tv_tab_name);
        tv_tabName.setTextSize(16);
        tv_tabName.getPaint().setFakeBoldText(true);
        tv_tabName.setTextColor(ContextCompat.getColor(mContext, R.color.main_color2));
        vline.setBackgroundColor(ContextCompat.getColor(mContext, R.color.main_color2));
    }

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

    }

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

    }
});

 



    
    

    

        

        

    


你可能感兴趣的:(Android)