美文网首页view
TabLayout.Tab设置文字+icon

TabLayout.Tab设置文字+icon

作者: 小鹿儿 | 来源:发表于2015-08-27 16:34 被阅读13510次

TabLayout是Google新出的一个控件,可以通过以下方式添加TabLayout.Tab

mTabLayout.addTab(mTabLayout.newTab().setText("Tab1"));
mTabLayout.addTab(mTabLayout.newTab().setText("Tab2"));

可如果Tab中既有文字,又有icon,且icon需要Tab的状态而改变,就比较麻烦了。我们可以使用SpannableString结合ImageSpan来实现。

    /**
     * 获取第三个tab的内容,其本质是把文字与icon结合在一起
     *
     * @param title 第三个tab栏需要显示的内容
     * @return
     */
    public CharSequence getThirdTabTitle(String title) {
        Drawable image = getResources().getDrawable(R.drawable.bg_down_arrow);
        image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
        // Replace blank spaces with image icon
        SpannableString sb = new SpannableString(title + "   ");
        ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
        sb.setSpan(imageSpan, title.length(), title.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        return sb;
    }

这样可以获取Tab需要显示的文本内容,进而通过setText (CharSequence text)方法设置显示内容。
或者根据不同的点击状态,直接设置Tab

    /**
     * 设置第三个tab的内容
     *
     * @param isTabSelected 第三个tab是否被点击
     */
    public void setThirdTab(boolean isTabSelected) {
        String title = "";
        //根据此时的tradingType来设置第三个tab栏的文字
        if (tradingType == -1) {
            title = "分类";
        } else {
            title = content[tradingType];
        }
        Drawable image;
        //根据第三个tab栏是否点击,设置不同的箭头方向
        if (isTabSelected) {
            image = getResources().getDrawable(R.drawable.down_arrow_press);
        } else {
            image = getResources().getDrawable(R.drawable.down_arrow_normal);
        }
        image.setBounds(0, 0, image.getIntrinsicWidth(), image.getIntrinsicHeight());
        // Replace blank spaces with image icon
        SpannableString sb = new SpannableString(title + "   ");
        ImageSpan imageSpan = new ImageSpan(image, ImageSpan.ALIGN_BOTTOM);
        sb.setSpan(imageSpan, title.length(), title.length() + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        mTabLayout.getTabAt(2).setText(sb);
    }

相关文章

网友评论

  • c60691962bed:<?xml version="1.0" encoding="utf-8" ?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android&quot;>
    <!-- 没有焦点时字体颜色 -->
    <item android:drawable="@drawable/include_top_contacts_normal" android:state_selected="false" />
    <!--选中时的字体颜色 -->
    <item android:drawable="@drawable/include_top_contacts_pressed" android:state_selected="true" />
    <!-- 非触摸模式下获得焦点并单击时的字体颜色 -->
    <item android:drawable="@drawable/include_top_contacts_pressed" android:state_focused="true" android:state_pressed="true" />
    </selector>
  • c60691962bed: private int[] mIconSelectIds= {R.drawable.contacts_top_icon_friends,
    R.drawable.contacts_top_icon_friends,
    R.drawable.contacts_top_icon_friends,
    R.drawable.contacts_top_icon_friends};



    for (int i = 0; i < tabLayout.getTabCount(); i++) {
    tabLayout.getTabAt(i).setText(mTitles[i]).setIcon(mIconSelectIds[i]).isSelected();
    }
  • 232b18abbbfe:写了个demo,发现只能显示Title文字内容,图片无法显示。
    dotdog:@232b18abbbfe 用SpannableString和ImageSpan确实不能显示图片。我也遇到这个问题了。怎么解决的?

本文标题:TabLayout.Tab设置文字+icon

本文链接:https://www.haomeiwen.com/subject/kcvhcttx.html