【问题标题】:How can I change colors in my StateListDrawable?如何更改 StateListDrawable 中的颜色?
【发布时间】:2013-11-20 18:35:27
【问题描述】:

我有以下按钮选择器(有两种状态,常规和按下):

<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 加载时从代码中访问和更改这两种状态的颜色。

我该怎么做?

【问题讨论】:

    标签: android android-layout xamarin.android xamarin android-ui


    【解决方案1】:
         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 会提供drawable的实际数量。
    • 我必须更新 StateListDrawable 中的笔画,这是自定义视图的 xml 的一部分。我试图将此逻辑应用于多个自定义视图。事实证明,您需要在StateListDrawable 上调用mutate(),否则您正在对共享资源进行更改。 developer.android.com/reference/android/graphics/drawable/…
    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2011-08-26
      • 1970-01-01
      • 1970-01-01
      • 2020-09-16
      • 2011-03-23
      • 2011-01-02
      • 2012-10-16
      • 2012-06-01
      • 2021-10-23
      相关资源
      最近更新 更多