繁体   English   中英

动态更改actionbar.tab文本(标题)颜色

[英]Dynamically change actionbar.tab text (title) color

我需要更改标签标题的颜色和/或阴影。 我已经尝试过了,但是没有用:

public void iluminarTab(String target) {
    ActionBar.Tab tab = actionBar.getTabAt(getTabPositionByTitle(target));
    SpannableString ss = new SpannableString(tab.getText().toString());
    ss.setSpan(
            new ShadowSpan(4, 0, 0, getResources().getColor(
                    R.color.irc_color_13_pink)), 0, ss.length(),
            Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
    tab.setText(ss);
}

private Integer getTabPositionByTitle(String title) {
    for (int i = 0; i < actionBar.getTabCount(); i++) {
        if (actionBar.getTabAt(i).getText().toString()
                .equalsIgnoreCase(title)) {
            return i;
        }
    }
    return null;
}

// NOTE: a separated class, posting here just to explain.
// this works on other spannables
public class ShadowSpan extends CharacterStyle {
    public float Dx;
    public float Dy;
    public float Radius;
    public int Color;

    public ShadowSpan(int radius, int dx, int dy, int color) {
        Radius = radius;
        Dx = dx;
        Dy = dy;
        Color = color;
    }

    @Override
    public void updateDrawState(TextPaint tp) {
        tp.setShadowLayer(Radius, Dx, Dy, Color);
    }
}

如果我可以设置textview属性,那也会对我有帮助。 有什么办法可以帮助我吗? 提前致谢。

您可以使用getCustomView()获取选项卡的视图,然后在其上调用setBackgroundResource()

public void onTabSelected(Tab tab, FragmentTransaction ft) {
    RelativeLayout tabLayout = (RelativeLayout) tab.getCustomView(); //get the view for the tab
    tabLayout.setBackgroundResource(R.id.desired_background_with_indicator); // change the background
    tab.setCustomView(tabLayout); // assign back to the tab
}

尽管我使用了onTabSelected ,但在您的情况下,您的方法中已经有对ActionBar.Tab的引用,请以相同的方式使用tab对象。

您可以将ColorDrawable用于背景或最适合的背景。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM