动态更改操作栏分隔线颜色(对于以编程方式生成的 ShapeDrawable 的 android:bottom 属性)?

4

我正在尝试以编程方式更改操作栏底部的分隔栏颜色。我的策略是将操作栏背景设置为包含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.
搞定了!非常感谢--请把这个评论作为答案发布,以便我能授予你赏金。 - user460847
4个回答

3

1
根据LayerDrawable的源代码,允许你这样做的方法是私有的。但是你可以使用一个持有InsetDrawable的LayerDrawable来完成这个操作。

0
这对我有用:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >

    <item android:id="@+id/actionBarLineParent">
        <shape
            android:id="@+id/actionBarLine"
            android:shape="rectangle" >
            <solid android:color="@color/red_all_files" />
        </shape>
    </item>

    <item
        android:id="@+id/actionBarColourParent"
        android:bottom="2dip">
        <shape
            android:id="@+id/actionBarColour"
            android:shape="rectangle" >
            <solid android:color="@android:color/white" />
        </shape>
    </item>

</layer-list>

然后调用:

final LayerDrawable ld = (LayerDrawable) getResources().getDrawable(R.drawable.actionbar_background);
ld.setDrawableByLayerId(R.id.actionBarLineParent, new ColorDrawable(Color.BLUE));
getActionBar().setBackgroundDrawable(ld);

你需要确保设置了android:id


0

如果您能详细说明您以编程方式更改颜色的原因,那就更好了。如果您没有控制背景颜色,这样做似乎有点毫无意义。如果您正在控制背景颜色,则可能正在使用样式,在这种情况下,我可以想到几种方法来实现此目的。

西蒙


我希望分隔栏的颜色能够随着用户所在的部分而改变,而且这些部分的颜色是在运行时从 Web 服务中获取的。我始终希望操作栏的背景颜色为黑色,但不包括分隔栏的颜色。 - user460847

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接