Android setState()如何控制可绘制文件的特定状态?

Android setState()如何控制可绘制文件的特定状态?,android,android-widget,Android,Android Widget,我有一张这样的画: <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:state_window_focused="true" android:drawable="@drawable/seek_thumb_pressed" /> <item android:state

我有一张这样的画:

<selector xmlns:android="http://schemas.android.com/apk/res/android">

<item android:state_pressed="true"
      android:state_window_focused="true"
      android:drawable="@drawable/seek_thumb_pressed" />

<item android:state_focused="true"
      android:state_window_focused="true"
      android:drawable="@drawable/seek_thumb_selected" />

<item android:state_selected="true"
      android:state_window_focused="true"
      android:drawable="@drawable/seek_thumb_selected" />

<item android:drawable="@drawable/seek_thumb_normal" />


在代码中,如何设置绘图表的特定状态?我想将其设置为状态_pressed=true状态

明白了。这里的评论有助于:

因此
Drawable.setState()
接受整数数组。这些整数表示可绘制图形的状态。你可以传递任何你想要的INT。一旦ints传入匹配项,状态列表中的“项”将可绘制,可绘制项将在该状态下绘制

它在代码中更有意义:

int[] state = new int[] {android.R.attr.state_window_focused, android.R.attr.state_focused};
minThumb.setState(state);

请注意,我的状态列表drawable既有
state\u pressed=“true”
又有
android:state\u window\u focused=“true”
。因此,我必须将这两个int值传递给
setState
。当我想清除它时,我只是
minThumb.setState(newint[]{})

没有足够的评论点,所以这只是android开发者所说的补充。对于某些状态,例如state_enabled,没有明显的方法设置该状态的相反值(例如,没有state_disabled)。要指示负的或相反的状态,只需传递该状态的资源的负值

int[] state = new int[] {-android.R.attr.state_enabled}; //Essentially state_disabled
然后像往常一样将该整数数组传递到setState()方法中

minThumb.setState(state);

如果当前视图没有背景,并且需要将其状态通知其子视图(因为它们有android:duplicateParentState=“true”)?您将如何更改视图的状态?“它在代码中更有意义”-与可绘制状态相关的任何内容都没有意义…minThumb是什么?