Как я могу изменить цвета в моем StateListDrawable?
У меня есть следующий селектор для кнопки (с 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>
Моя кнопка имеет следующий, который определяет фон как вышеупомянутый селектор:
<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" />
Мне нужно получить доступ и изменить цвета для обоих состояний из кода, когда загружается активность.
Как я могу это сделать?
3 ответа
Решение
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);
Я использовал это, чтобы изменить ход, это может быть полезно.
Это версия ответа @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)
и да! Большое спасибо @Borja. Я обнаружил, что это работает хорошо, как и в 2022 году.
У меня есть ниже селектор 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"))