Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/199.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 以编程方式更改图层列表中形状的颜色_Android_Shape_Android Drawable_Layerdrawable - Fatal编程技术网

Android 以编程方式更改图层列表中形状的颜色

Android 以编程方式更改图层列表中形状的颜色,android,shape,android-drawable,layerdrawable,Android,Shape,Android Drawable,Layerdrawable,我试图以编程方式更改可选的\u kachel\u形状的颜色。以下是xml文件: kachel\u ticked\u style.xml <layer-list xmlns:android="http://schemas.android.com/apk/res/android" > <item> <shape android:id="@+id/selectable_kachel_shape"

我试图以编程方式更改可选的\u kachel\u形状的颜色。以下是xml文件:

kachel\u ticked\u style.xml

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >


    <item>
        <shape
            android:id="@+id/selectable_kachel_shape"
            android:shape="rectangle" >
            <stroke
                android:width="5dp"
                android:color="@color/headrbar_color" />
        </shape>
    </item>
    <item>
        <rotate
            android:fromDegrees="45"
            android:pivotX="120%"
            android:pivotY="100%"
            android:toDegrees="45" >
            <shape android:shape="line" >
                <stroke
                    android:width="40dp"
                    android:color="@color/headrbar_color" />
            </shape>
        </rotate>
    </item>
    <item
        android:right="5dp"
        android:top="5dp">
        <bitmap
            android:gravity="top|right"
            android:src="@drawable/selectable_tiles_check" />
    </item>

</layer-list>
1.为什么我在shape.setColor中获取NullPointerException


2.如何以编程方式更改形状(即图层列表)内的颜色?

好的,我找到了答案,我只需将形状的id放在项目内,而不是形状内 kachel\u ticked\u style.xml

<item 
    android:id="@+id/selectable_kachel_shape">
        <shape
             android:shape="rectangle" >
            <stroke
                android:width="5dp"
                android:color="@color/headrbar_color" />
        </shape>
    </item>


然后,您可以更改调用shape.setColor的形状的颜色或调用shape.setStroke的笔划的颜色(strokeWidth,strokeColor)

但我也有同样的问题。这对meWell@jincyabraham不起作用。你可以先获取视图,然后获取背景可绘制,然后将形状作为GradientDrawable,如图所示。你可以展示你为获取形状对象所做的步骤吗,目前还不清楚。这对GradientDrawable有效,但对于我试图在运行时定义的可旋转可移动颜色,则不适用。有什么想法吗?
<item 
    android:id="@+id/selectable_kachel_shape">
        <shape
             android:shape="rectangle" >
            <stroke
                android:width="5dp"
                android:color="@color/headrbar_color" />
        </shape>
    </item>