gpt4 book ai didi

android-layout - 如何更改 StateListDrawable 中的颜色?

转载 作者:行者123 更新时间:2023-12-01 16:49:40 27 4
gpt4 key购买 nike

我有以下按钮选择器(有 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" />

当 Activity 加载时,我需要从代码中访问和更改两种状态的颜色。

我该怎么做?

最佳答案

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

我用它来改变笔画,它会很有帮助。

关于android-layout - 如何更改 StateListDrawable 中的颜色?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61716100/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com