12

I have the following selector for a button (with 2 states, regular and pressed):

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

How can I do this?

4

2 回答 2

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

我用它来改变笔画,它可能会有所帮助。

于 2014-07-30T14:06:44.290 回答
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"))
于 2018-11-19T11:28:53.893 回答