简体   繁体   English

如何更改可绘制波纹的纯色?

[英]how do I change the solid color of a ripple drawable?

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
        android:color="?android:attr/colorControlHighlight">
    <item>
        <selector>
            <item android:state_selected="true">
                <layer-list>
                    <item android:left="-5dp"
                          android:top="-5dp"
                          android:right="-5dp">
                        <shape android:shape="rectangle">
                            <stroke android:width="3dp"
                                    android:color="@android:color/white"/>
                            <solid android:color="@android:color/transparent"/>
                        </shape>
                    </item>
                </layer-list>
            </item>
            <item android:state_selected="false">
                <shape android:shape="rectangle">
                    <solid android:color="@android:color/transparent"/>
                </shape>
            </item>

        </selector>
    </item>
</ripple>

here is my ripple drawable and i want to change the state_selected , solid color. 这是我的涟漪可绘制,我想更改state_selected纯色

code i've tried: 我尝试过的代码:

RippleDrawable rippleDrawable = (RippleDrawable) textView.getBackground(); // assumes bg is a RippleDrawable
        int[][] states = new int[][]{new int[]{android.R.attr.state_selected}};
        int[] colors = new int[]{R.color.white}; 
        ColorStateList colorStateList = new ColorStateList(states, colors);
        rippleDrawable.setColor(colorStateList);

unfortuantely it doesn't work.. what am I missing and is this possible? 不幸的是,它不起作用..我想念什么,这可能吗?

You should add an id to items to access them via java/kotlin. 您应该在项目中添加一个ID,以便通过java / kotlin访问它们。
check this background XML file 检查此背景XML文件

<?xml version="1.0" encoding="utf-8"?>
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
    android:color="#e0e0e0">
   <item android:id="@+id/fab_shape">
      <shape xmlns:android="http://schemas.android.com/apk/res/android"
         android:shape="rectangle">
         <corners android:radius="25dp" />
        <solid android:color="@color/colorAccent" />
     </shape>
  </item>
</ripple>

to change the solid color of this, on constraintLayout background, this drawable XML is applied 要更改其纯色,在constraintLayout背景上,将应用此可绘制的XML

val background = constraintLayout.background as RippleDrawable
val bgShape = background.findDrawableByLayerId(R.id.fab_shape) as GradientDrawable
bgShape.color = color

for reference read this 供参考阅读

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

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