android:如何使用普通/按下状态可编程地创建一个drawable

时间:2015-02-05 11:25:06

标签: android drawable

在.xml中指定具有正常/按下状态的视图背景是很简单的,如

<?xml version="1.0" encoding="utf-8"?>  
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
    <item android:state_focused="false" 
        android:state_enabled="true" android:state_pressed="false"  
        android:color="@color/setting_color_select" />  
    <item android:state_pressed="true" android:color="@color/setting_color_select" />  

</selector>  

但是如何在指定焦点或按下状态的情况下创建可绘制的可绘制?我在开发者文档中找不到答案。

1 个答案:

答案 0 :(得分:0)

StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},
         getResources().getDrawable(R.drawable.pressed));
states.addState(new int[] {android.R.attr.state_focused},
         getResources().getDrawable(R.drawable.focused));
states.addState(new int[] { },
         getResources().getDrawable(R.drawable.normal));
imageView.setImageDrawable(states);