«   2025/01   »
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31
Tags
more
Archives
Today
Total
관리 메뉴

올해는 머신러닝이다.

XML Selector를 자바코드로 변경 본문

Android/Tip&Tech

XML Selector를 자바코드로 변경

행복한 수지아빠 2013. 8. 13. 11:02

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/iconSelector">
 
<!-- pressed -->
 
<item android:state_pressed="true" android:drawable="@drawable/btn_icon_hl" />
 
<!-- focused -->
 
<item android:state_focused="true" android:drawable="@drawable/btn_icon_hl" />
 
<!-- default -->
 
<item android:drawable="@drawable/btn_icon" />
</selector>

를 코드로 구현하기

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);