Android SlidingTabLayout选中标签字体粗体

时间:2015-08-20 05:55:30

标签: android

如何在SlidingTabLayout中将选定标签的字体加粗?我在设置选定的选项卡颜色时找到了足够的问题,但在设置选定的选项卡字体时没有找到。提前谢谢。

3 个答案:

答案 0 :(得分:2)

您可以使用方法将TabLayout.OnTabSelectedListener添加到TabLayout addOnTabSelectedListener(TabLayout.OnTabSelectedListener listener)。 并覆盖这样的方法:

@Override
public void onTabSelected(TabLayout.Tab tab) {
    LinearLayout tabLayout = (LinearLayout)((ViewGroup) mMainTabs.getChildAt(0)).getChildAt(tab.getPosition());
    TextView tabTextView = (TextView) tabLayout.getChildAt(1);
    tabTextView.setTypeface(tabTextView.getTypeface(), Typeface.BOLD);
}

@Override
public void onTabUnselected(TabLayout.Tab tab) {
    LinearLayout tabLayout = (LinearLayout)((ViewGroup) mMainTabs.getChildAt(0)).getChildAt(tab.getPosition());
    TextView tabTextView = (TextView) tabLayout.getChildAt(1);
    tabTextView.setTypeface(tabTextView.getTypeface(), Typeface.NORMAL);
}

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

答案 1 :(得分:1)

SlidingTabLayout.java 中,您可以看到 createDefaultTabView(上下文关联)功能,您可以在此处设置自定义字体:

protected TextView createDefaultTabView(Context context) {
    TextView textView = new TextView(context);
    textView.setGravity(Gravity.CENTER);
    textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, TAB_VIEW_TEXT_SIZE_SP);
    textView.setTypeface(Typeface.DEFAULT_BOLD);
    textView.setTextColor(getResources().getColorStateList(R.color.main));
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // If we're running on Honeycomb or newer, then we can use the Theme's
        // selectableItemBackground to ensure that the View has a pressed state
        TypedValue outValue = new TypedValue();
        getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground,
                outValue, true);
        textView.setBackgroundResource(outValue.resourceId);
    }

    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
        // If we're running on ICS or newer, enable all-caps to match the Action Bar tab style
        textView.setAllCaps(true);
    }

    int padding = (int) (TAB_VIEW_PADDING_DIPS * getResources().getDisplayMetrics().density);
    textView.setPadding(padding, padding, padding, padding);

    return textView;
}

setTypeface 位于上述代码段的第5行

答案 2 :(得分:1)

我最终按如下方式管理:

onPageScrolled()方法中进行以下更改:

View selectedTitle = mTabStrip.getChildAt(position);
        View leftOfSelected = mTabStrip.getChildAt(position-1);
        View rightOfSelected=null;
        if (position<=tabStripChildCount-1) {
            rightOfSelected = mTabStrip.getChildAt(position + 1);
        }

        if (selectedTitle != null) {
            TextView selectedText = (TextView) selectedTitle;
            TextView leftOfSelectedText = (TextView) leftOfSelected;
            TextView rightOfSelectedText = (TextView) rightOfSelected;
            if (position > 0 && position < tabStripChildCount - 1) {
                selectedText.setTypeface(Typeface.DEFAULT_BOLD);
                leftOfSelectedText.setTypeface(Typeface.DEFAULT);
                rightOfSelectedText.setTypeface(Typeface.DEFAULT);
            } else if (position == 0) {
                selectedText.setTypeface(Typeface.DEFAULT_BOLD);
                rightOfSelectedText.setTypeface(Typeface.DEFAULT);

            } else if (position == tabStripChildCount - 1) {
                selectedText.setTypeface(Typeface.DEFAULT_BOLD);
                leftOfSelectedText.setTypeface(Typeface.DEFAULT);
            }
        }

同样,可以使用与上面相同的代码段编辑scrollToTab()方法。您需要做的就是将selectedTitle替换为selectedChild,将position替换为tabIndex。如果您想要更好的UI响应,也可以在onPageSelected()方法中执行相同的操作。请记住重新声明tabStripChildCount

干杯!

修改

随着TabLayout的推出,这变得更加容易。请参阅Alex Zatsepin的答案。