我该如何在StateListDrawable中更改颜色?

13

我有一个针对一个具有两种状态(正常和按下)的按钮的选择器:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" >
 <shape>
  <solid
      android:color="#3498DB" />
  <stroke
      android:width="1dp"
      android:color="#2980B9" />
  <corners
      android:radius="0dp" />
  <padding
      android:left="12dp"
      android:top="12dp"
      android:right="12dp"
      android:bottom="12dp" />
 </shape>
</item>
<item>
 <shape>
  <solid
      android:color="#2980B9" />
  <stroke
      android:width="1dp"
      android:color="#2980B9" />
  <corners
      android:radius="0dp" />
  <padding
      android:left="12dp"
      android:top="12dp"
      android:right="12dp"
      android:bottom="12dp" />
 </shape>
</item>
</selector>

我的按钮具有以下功能,可以将背景指定为上述选择器:
<Button
  android:id="@+id/button_LaunchShiftsGame"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:text="@string/ShiftsMode"
  android:background="@drawable/selector_Button"
  style="@style/Button_Text" />

当 Activity 加载时,我需要从代码中访问并更改两个状态的颜色。

我应该如何做?

3个回答

34
     StateListDrawable gradientDrawable = (StateListDrawable) inflatedView.getBackground();
    DrawableContainerState drawableContainerState = (DrawableContainerState) gradientDrawable.getConstantState();
    Drawable[] children = drawableContainerState.getChildren();
    LayerDrawable selectedItem = (LayerDrawable) children[0];
    LayerDrawable unselectedItem = (LayerDrawable) children[1];
    GradientDrawable selectedDrawable = (GradientDrawable) selectedItem.getDrawable(0);
    GradientDrawable unselectedDrawable = (GradientDrawable) unselectedItem.getDrawable(0);
    selectedDrawable.setStroke(STORKE_SIZE, NOTIFICATION_COLOR);
    unselectedDrawable.setStroke(STORKE_SIZE, NOTIFICATION_COLOR);

我用这个来改变描边,它可能会有用。


请注意,为了迭代子数组,请使用getChildCount而不是children.lengthgetChildCount将提供可绘制对象的实际数量。 - Cheok Yan Cheng
我必须更新自定义视图xml中的StateListDrawable的描边。我试图将此逻辑应用于多个自定义视图。结果发现,您需要在StateListDrawable上调用mutate(),否则您会对共享资源进行更改。 https://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html#mutate() - Etienne Lawlor

2
这是带有可绘制资源的 Kotlin 版本 @Borja's answer
val drawableRes: Drawable? = ResourcesCompat.getDrawable(mContext.resources, R.drawable.drawable_res_id, null)

val drawableContainerState: DrawableContainer.DrawableContainerState = drawableRes?.constantState as DrawableContainer.DrawableContainerState

val children: Array<Drawable> = drawableContainerState.children

val selectedItem: GradientDrawable = children[0] as GradientDrawable

val unselectedItem: GradientDrawable = children[1] as GradientDrawable

selectedItem.setStroke(2, colorState)
unselectedItem.setStroke(2, colorState)

"是的!非常感谢@Borja。 我发现这在2022年运作良好。"

0

我有以下选择器round_circle_blue.xml形状xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
    <shape android:shape="oval">
        <size android:width="@dimen/_50sdp" android:height="@dimen/_50sdp" />
        <solid android:color="@color/color_blue" />
        <stroke android:width="@dimen/_2sdp" android:color="@color/white" />
    </shape>
</item>
</selector>

然后我将其设置为文本视图的背景,如下所示

<TextView
    android:id="@+id/tvActivityicon"
    android:layout_width="@dimen/_40sdp"
    android:layout_height="@dimen/_40sdp"
    android:textColor="@color/white"
    android:gravity="center"
    android:text="A"
    android:textSize="@dimen/_14sdp"
    android:background="@drawable/round_circle_blue" />

在 Kotlin 文件中,我使用以下代码来更改颜色。
val gradientDrawable = tvActivityicon.background as StateListDrawable
    val drawableContainerState = gradientDrawable.constantState as DrawableContainer.DrawableContainerState
    val children = drawableContainerState!!.children
    val selectedItem = children[0] as GradientDrawable
    selectedItem.setColor(Color.parseColor("#FD00DF"))

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接