【问题标题】:Dynamically change actionbar.tab text (title) color动态更改 actionbar.tab 文本(标题)颜色
【发布时间】:2014-04-11 01:47:40
【问题描述】:

我需要更改标签标题的颜色和/或阴影。 我试过了,还是不行:

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 属性,它也会对我有所帮助。 有什么想法可以帮助我吗? 提前致谢。

【问题讨论】:

    标签: android colors tabs android-actionbar title


    【解决方案1】:

    您可以使用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 作为背景或任何最适合您的方式。

    【讨论】:

    • getCustomView 返回 null;
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-24
    • 1970-01-01
    相关资源
    最近更新 更多