简体   繁体   English

在可绘制选择器中更改实体的颜色

[英]Change colors of solid in drawable selector

I have the following drawable: 我有以下可绘制对象:

<item android:state_pressed="true" android:text="texting .....">
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
        <stroke android:width="@dimen/circleStrokeThickness" android:color="@color/earlGreen" />
        <solid
            android:color="@color/lightGrey"/>

        <size
            android:width="100dp"
            android:height="100dp"/>
    </shape>
</item>


<item android:state_pressed="false" android:text="texting .....">
    <shape
        xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval">
        <stroke android:width="4dp" android:color="@color/earlGreen" />
        <solid
            android:color="@android:color/white"/>

        <size
            android:width="100dp"
            android:height="100dp"/>
    </shape>
</item>

However, I need a number of similar one where the solid color is different but the rest is the same. 但是,我需要一些类似的颜色,其中纯色不同,但其余颜色相同。 Is there any easy way to do this or should I just define a number of xml files? 有什么简单的方法可以做到这一点,还是应该只定义一些xml文件? I know it is possible to change the background color at runtime but I can't see how to get to the colour of a specific state. 我知道可以在运行时更改背景颜色,但是我看不到如何获得特定状态的颜色。

You can define id for shape item <item android:id="@+id/shape_bacground"../> then at runtime you have to get background of your view and cast it to LayerDrawable and use findDrawableByLayerId() for find your shape and set it's color using setColor() . 您可以为形状项目<item android:id="@+id/shape_bacground"../>定义ID,然后在运行时必须获取视图背景并将其转换为LayerDrawable并使用findDrawableByLayerId()查找形状并使用setColor()设置颜色。 Here is sample code: 这是示例代码:

drawable xml 可绘制的xml

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

    <item android:drawable="@drawable/float_button_shadow1">
    </item>
    <item
        android:id="@+id/shape_bacground"
        android:bottom="2dp"
        android:left="1dp"
        android:top="1dp"
        android:right="1dp">
        <shape android:shape="oval" >
            <solid android:color="#1E88E5" />
        </shape>
    </item>

</layer-list>

Changing color 变色

try {
        LayerDrawable layer = (LayerDrawable) view.getBackground();

        GradientDrawable shape = (GradientDrawable) layer
                .findDrawableByLayerId(R.id.shape_bacground);
        shape.setColor(backgroundColor);// set new background color here
        rippleColor = makePressColor();
    } catch (Exception ex) {
        // Without bacground
    }

You can do same thing by programatically, Where you can change any color at runtime by making function and passing Color in parameter. 您可以通过编程来执行相同的操作,在其中可以通过运行函数并在参数中传递Color来在运行时更改任何颜色。

ShapeDrawable sd1 = new ShapeDrawable(new RectShape());
                            sd1.getPaint().setColor(CommonUtilities.color);
                            sd1.getPaint().setStyle(Style.STROKE);
                            sd1.getPaint().setStrokeWidth(CommonUtilities.stroke);
                            sd1.setPadding(15, 10, 15, 10);

                            sd1.getPaint().setPathEffect(
                                    new CornerPathEffect(CommonUtilities.corner));
                            ln_back.setBackgroundDrawable(sd1);

Hope it will help you ! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM