Android Button 原理
2010-11-05 00:56:20 来源:WEB开发网9. * @param stateSet - An array of resource Ids to associate with the image.
10. * Switch to this image by calling setState().
11. * @param drawable -The image to show.
12. */
13. public void addState(int[] stateSet, Drawable drawable) {
14. if (drawable != null) {
15. mStateListState.addStateSet(stateSet, drawable);
16. // in case the new state matches our current state...
17. onStateChange(getState());
18. }
19. }
20. ...
21. }
xml里面每一个Item就对应一种状态,而每一个有state_的属性就是一个状态的描述,drawable则是真正的drawable图片(这个其实也可以是其他类型的Drawable实例)。
当把这个实例付给View作为Background的时候,View会根据不同的state来切换不同状态的图片,从而实现了Press等诸多效果。简单看一下View中有关状态切换的代码如下:
各种View的状态列表:
1. /**
2. * The order here is very important to {@link #getDrawableState()}
3. */
4. private static final int[][] VIEW_STATE_SETS = {
5. EMPTY_STATE_SET, // 0 0 0 0 0
6. WINDOW_FOCUSED_STATE_SET, // 0 0 0 0 1
7. SELECTED_STATE_SET, // 0 0 0 1 0
8. SELECTED_WINDOW_FOCUSED_STATE_SET, // 0 0 0 1 1
9. FOCUSED_STATE_SET, // 0 0 1 0 0
10. FOCUSED_WINDOW_FOCUSED_STATE_SET, // 0 0 1 0 1
11. FOCUSED_SELECTED_STATE_SET, // 0 0 1 1 0
12. FOCUSED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 0 0 1 1 1
13. ENABLED_STATE_SET, // 0 1 0 0 0
14. ENABLED_WINDOW_FOCUSED_STATE_SET, // 0 1 0 0 1
15. ENABLED_SELECTED_STATE_SET, // 0 1 0 1 0
16. ENABLED_SELECTED_WINDOW_FOCUSED_STATE_SET, // 0 1 0 1 1
17. ENABLED_FOCUSED_STATE_SET, // 0 1 1 0 0
18. ENABLED_FOCUSED_WINDOW_FOCUSED_STATE_SET, // 0 1 1 0 1
19. ENABLED_FOCUSED_SELECTED_STATE_SET, // 0 1 1 1 0
更多精彩
赞助商链接