0

私は現在、次の 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>

4

1 に答える 1

1

問題がありました。それを検索しているときに、あなたの投稿が回答されていないことがわかったので、私の解決策をあなたと共有したいと思います. それはあなたを助けることができるかもしれません。

レイヤー リスト ドローアブルを作成し、そのアイテムの色を動的に変更しました。コードは次のとおりです。

<?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>

次のコードは、実行時に上記のドローアブルを変更して、色を変更します。

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);
于 2015-07-22T12:11:35.207 に答える