我正在尝试以编程方式更改操作栏底部的分隔栏颜色。我的策略是将操作栏背景设置为包含ShapeDrawable
矩形的以编程方式生成的LayerDrawable
,该矩形基于此XML:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Bottom Line -->
<item>
<shape android:shape="rectangle">
<solid android:color="@color/action_bar_line_color" />
</shape>
</item>
<!-- Color of your action bar -->
<item android:bottom="2dip">
<shape android:shape="rectangle">
<solid android:color="@color/action_bar_color" />
</shape>
</item>
</layer-list>
但我遇到了一个难题:我无法弄清如何以编程方式应用
android:bottom
属性(如 <item android:bottom="2dip">
)。显然,android:bottom
是 item 标签的属性,我认为没有任何程序化等效物,并且我也没有找到任何适当的 ShapeDrawable
方法/属性。到目前为止的代码:
public LayerDrawable createABBackground(String color) {
ShapeDrawable rect = new ShapeDrawable(new RectShape());
rect.getPaint().setColor(Color.parseColor("#000000"));
ShapeDrawable rect2 = new ShapeDrawable(new RectShape());
rect2.getPaint().setColor(Color.parseColor(color));
ShapeDrawable[] layers = {rect, rect2};
LayerDrawable background = new LayerDrawable(layers);
return background;
}
有什么想法吗?如有关于其他解决方案的问题,我正在使用ActionBarSherlock。
编辑:
根据MH的建议,setLayerInset实现了我想要的效果。以下是使用它进行修改的函数版本:
public LayerDrawable createABBackground(String color) {
ShapeDrawable rect = new ShapeDrawable(new RectShape());
rect.getPaint().setColor(Color.parseColor(color));
ShapeDrawable rect2 = new ShapeDrawable(new RectShape());
rect2.getPaint().setColor(Color.parseColor("#000000"));
ShapeDrawable[] layers = {rect, rect2};
LayerDrawable background = new LayerDrawable(layers);
background.setLayerInset(0, 0, 3, 0, 0);
background.setLayerInset(1, 0, 0, 0, 3);
return background;
}
setLayerInset(int index, int l, int t, int r, int b)
了吗?文档中说:“为drawable[index]指定边界修饰符。 left += l top += t; right -= r; bottom -= b;” - MH.