Android 动态更改actionbar.tab文本(标题)颜色
我需要更改选项卡标题的颜色和/或阴影。 我试过了,但不起作用:Android 动态更改actionbar.tab文本(标题)颜色,android,colors,tabs,android-actionbar,title,Android,Colors,Tabs,Android Actionbar,Title,我需要更改选项卡标题的颜色和/或阴影。 我试过了,但不起作用: public void iluminarTab(String target) { ActionBar.Tab tab = actionBar.getTabAt(getTabPositionByTitle(target)); SpannableString ss = new SpannableString(tab.getText().toString()); ss.setSpan( ne
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);
}
}
public void iluminarTab(字符串目标){
Tab=ActionBar.getTabAt(getTabPositionByTitle(target));
SpannableString ss=新的SpannableString(tab.getText().toString());
不锈钢固定盘(
新的阴影范围(4,0,0,getResources().getColor(
R.color.irc_color_13_pink)),0,ss.length(),
SPAN_独占性SPAN_独占性);
tab.setText(ss);
}
私有整数getTabPositionByTitle(字符串标题){
for(int i=0;i
如果我可以设置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
对象
您可以使用
彩色绘图
作为背景或任何最适合您的工具。getCustomView返回空值;