以编程方式创建具有旋转形状的层列表



我目前正在尝试将以下XML转换为以编程方式创建,这样我就可以以编程方式处理形状颜色,而不必用不同的颜色编码3个不同的相同形状。

<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/secondaryProgress">
    <shape
        android:innerRadiusRatio="3"
        android:shape="ring"
        android:thicknessRatio="14.0"
        android:useLevel="true">
        <solid android:color="@android:color/transparent" />
    </shape>
</item>
<item android:id="@android:id/progress">
    <rotate
        android:fromDegrees="270"
        android:pivotX="50%"
        android:pivotY="50%"
        android:toDegrees="270">
        <shape
            android:innerRadiusRatio="3"
            android:shape="ring"
            android:thicknessRatio="14.0"
            android:useLevel="true">
            <corners android:radius="10dp" />
            <rotate
                android:fromDegrees="0"
                android:pivotX="50%"
                android:pivotY="50%"
                android:toDegrees="360" />
            <solid android:color="@color/custom_color0" />
            <stroke
                android:width="1dip"
                android:color="@color/custom_color0" />
        </shape>
    </rotate>
</item>

我遇到了一些问题。在搜索时发现你的帖子没有得到回复,所以我想和你分享我的解决方案。也许它能帮助你。

我创建了一个可绘制的图层列表,并动态更改了它的项目颜色。这是代码:

<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="@+id/item_bottom_stroke" >
    <shape android:shape="rectangle">
        <solid android:color="#0096FF"/>
    </shape>
</item>
<item android:id="@+id/item_navbar_background" android:bottom="1dp" >
    <shape android:shape="rectangle">
        <solid android:color="#FFFFFF"/>
    </shape>
</item>

以下代码在运行时修改上面的drawable以更改其颜色。

LayerDrawable layerDrawable = (LayerDrawable) v.getContext().getResources().getDrawable(R.drawable.layer_list_navigation_with_border);
GradientDrawable strokeDrawable = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.item_bottom_stroke);
strokeDrawable.setColor(strokeColor[0]);
GradientDrawable backgroundColor = (GradientDrawable) layerDrawable.findDrawableByLayerId(R.id.item_navbar_background);
backgroundColor.setColors(bgColor);

最新更新