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

前端 未结 1 1584
萌比男神i
萌比男神i 2021-01-16 22:51



        
相关标签:
1条回答
  • 2021-01-16 23:08

    You should add an id to items to access them via java/kotlin.
    check this background XML file

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

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

    for reference read this

    0 讨论(0)
提交回复
热议问题