我有一个可绘制的状态列表,我想从可绘制的状态列表中获取特定的可绘制:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" xmlns:kplus="http://schemas.android.com/apk/res-auto">
<item kplus:key_type_space_alt="true" android:state_pressed="true" android:drawable="@drawable/space_1_pressed" />
<item kplus:key_type_space_alt="true" android:drawable="@drawable/space_1_normal" />
<!-- TopNav keys. -->
<item kplus:key_type_topnav="true" android:state_pressed="true" android:drawable="@drawable/tab_down" />
<item kplus:key_type_topnav="true" android:state_selected="true" android:drawable="@drawable/tab_down" />
<item kplus:key_type_topnav="true" android:drawable="@drawable/tab_normal" />
<!-- TopRow keys. -->
<item kplus:key_type_toprow="true" android:state_pressed="true" android:drawable="@drawable/numeric_presseed" />
<item kplus:key_type_toprow="true" android:drawable="@drawable/numeric_normal" />
</selector>
我为每个键选择正确的可绘制状态,如下所示:
if (keyIsNumbers) {
if (KPlusInputMethodService.sNumbersState == 2) {
drawableState = mDrawableStatesProvider.KEY_STATE_TOPNAV_CHECKED;
}
}
现在状态定义如下:
KEY_STATE_TOPNAV_NORMAL = new int[] {keyTypeTopNavAttrId};
KEY_STATE_TOPNAV_PRESSED = new int[] {keyTypeTopNavAttrId, android.R.attr.state_pressed};
KEY_STATE_TOPNAV_CHECKED = new int[] {keyTypeTopNavAttrId, android.R.attr.state_selected};
现在我的问题是如何为每个状态提取正确的可绘制对象?我需要获取drawable的9patch填充,因为如果状态在9patch上有不同的填充,它将仅获得顶部drawable的填充,并且我想为每个键手动设置填充(drawable.getPadding(rect)) .