我有一个针对一个具有两种状态(正常和按下)的按钮的选择器:
<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 加载时,我需要从代码中访问并更改两个状态的颜色。
我应该如何做?
getChildCount
而不是children.length
。getChildCount
将提供可绘制对象的实际数量。 - Cheok Yan ChengStateListDrawable
的描边。我试图将此逻辑应用于多个自定义视图。结果发现,您需要在StateListDrawable
上调用mutate()
,否则您会对共享资源进行更改。 https://developer.android.com/reference/android/graphics/drawable/StateListDrawable.html#mutate() - Etienne Lawlor