更改颜色的形状里面一层列表编程 [英] Changing color in a shape inside a layer-list programmatically

查看:197
本文介绍了更改颜色的形状里面一层列表编程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图改变编程的 selectable_kachel_shape 的颜色。下面是XML文件:

I am trying to change programmatically the color of the selectable_kachel_shape. Here is the xml file:

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

我打电话内的片段以下code

I am calling inside a Fragment the following code

LayerDrawable layers = (LayerDrawable)  this.getActivity().getResources().getDrawable(R.drawable.kachel_ticked_style);

GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.selectable_kachel_shape));
                       shape.setColor(this.getActivity().getResources().getColor(android.R.color.background_dark);

1.为什么我在歌厅一个NullPointerException异常中的 shape.setColor

2.How,才有可能改变形状内的颜色,这是一个图层列表里面,编程?

2.How would it be possible to change the color inside a shape, that is inside a layer list, programmatically?

推荐答案

好吧,我找到了答案,我只是把形状标识的项里没有在造型 kachel_ticked_style.xml

Ok I found the answer, I just had to put the id of the shape inside the item not in the shape kachel_ticked_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)颜色

这篇关于更改颜色的形状里面一层列表编程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆