动态更改操作栏分隔颜色(android:bottom用于以编程方式生成的ShapeDrawable)?
作者:互联网
我正在尝试以编程方式更改操作栏底部的分隔栏的颜色.我的策略是根据this XML将操作栏背景设置为包含ShapeDrawable矩形的以编程方式生成的LayerDrawable:
<?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)
了吗?文档说:
Specify modifiers to the bounds for the drawable[index]. left += l top
+= t; right -= r; bottom -= b;
标签:android,actionbarsherlock,android-actionbar,shapedrawable 来源: https://codeday.me/bug/20190625/1283625.html