简体   繁体   English

如何在我的 StateListDrawable 中更改 colors?

[英]How can I change colors in my StateListDrawable?

I have the following selector for a button (with 2 states, regular and pressed):我有以下按钮选择器(有 2 种状态,常规和按下):

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

My button has the following which specifies the background as the above 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" />

I need to access and change the colors for both states from code when the Activity loads.当活动加载时,我需要从代码访问和更改两种状态的 colors。

How can I do this?我怎样才能做到这一点?

     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);

I used this to change the stroke, it can be helpfull.我用它来改变笔画,它会有所帮助。

This is the Kotlin version of the @Borja's answer with drawable resource这是带有可绘制资源的@Borja答案的 Kotlin 版本

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)

and yes.是的。 Many thanks to @Borja.非常感谢@Borja。 I found this working well as in 2022.我发现这在 2022 年运作良好。

I have below selector round_circle_blue.xml shape xml我有下面的选择器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>

And I have set it as background in text view as below我已将其设置为文本视图中的背景,如下所示

<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" />

and in kotlin file i have use below code to change the color.在 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"))

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

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