xamarin 如何更改StateListDrawable中的颜色?

m1m5dgzv  于 2022-12-07  发布在  其他
关注(0)|答案(3)|浏览(128)

我有以下按钮选择器(有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具有以下内容,它将背景指定为上述选择器:

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

我需要在“活动”加载时从代码中访问并更改这两种状态的颜色。
我该怎么做?

omvjsjqw

omvjsjqw1#

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

我用这个来改变笔划,它会很有帮助。

j13ufse2

j13ufse22#

这是@博尔哈答案的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)

是的!非常感谢@博尔哈。我发现这和2022年一样运行良好。

crcmnpdw

crcmnpdw3#

我有下面的选择器round_circle_blue.xmlshape 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"))

相关问题