지식쌓기
Android - Change Tab Background
바나나쥬스
2009. 5. 21. 13:12
TabWidget에서 추가되는 Tab의 Background변경하기
Tab마다 View를 얻어와서 직접 BackgroundDrawable을 지정하고
아래 막대부분은 reflection을 이용하여 꽁수로 바꿔치기 한다
tab_indicator.xml, tab_bar_left.xml, tab_bar_right.xml 내용은 <selector>로 정의
tab_indicator.xml 예
Tab마다 View를 얻어와서 직접 BackgroundDrawable을 지정하고
아래 막대부분은 reflection을 이용하여 꽁수로 바꿔치기 한다
tab_indicator.xml, tab_bar_left.xml, tab_bar_right.xml 내용은 <selector>로 정의
private void changeTabWidgetStyle(TabWidget tw){
for (int i = 0; i < tw.getChildCount(); i++) {
View v = tw.getChildAt(i);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_indicator));
}
Field mBottomLeftStrip;
Field mBottomRightStrip;
try {
mBottomLeftStrip = tw.getClass().getDeclaredField("mBottomLeftStrip");
mBottomRightStrip = tw.getClass().getDeclaredField("mBottomRightStrip");
if(!mBottomLeftStrip.isAccessible()) {
mBottomLeftStrip.setAccessible(true);
}
if(!mBottomRightStrip.isAccessible()){
mBottomRightStrip.setAccessible(true);
}
mBottomLeftStrip.set(tw, getResources().getDrawable(R.drawable.tab_bar_left));
mBottomRightStrip.set(tw, getResources().getDrawable(R.drawable.tab_bar_right));
} catch (Exception e) {
e.printStackTrace();
}
}
for (int i = 0; i < tw.getChildCount(); i++) {
View v = tw.getChildAt(i);
v.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_indicator));
}
Field mBottomLeftStrip;
Field mBottomRightStrip;
try {
mBottomLeftStrip = tw.getClass().getDeclaredField("mBottomLeftStrip");
mBottomRightStrip = tw.getClass().getDeclaredField("mBottomRightStrip");
if(!mBottomLeftStrip.isAccessible()) {
mBottomLeftStrip.setAccessible(true);
}
if(!mBottomRightStrip.isAccessible()){
mBottomRightStrip.setAccessible(true);
}
mBottomLeftStrip.set(tw, getResources().getDrawable(R.drawable.tab_bar_left));
mBottomRightStrip.set(tw, getResources().getDrawable(R.drawable.tab_bar_right));
} catch (Exception e) {
e.printStackTrace();
}
}
tab_indicator.xml 예
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/tab_unselected" />
<item android:state_focused="false"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/tab_selected" />
<!-- Focused states -->
<item android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/tab_focus" />
<item android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/tab_focus" />
<!-- Pressed -->
<item android:state_pressed="true" android:drawable="@drawable/tab_press" />
</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states -->
<item android:state_focused="false"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/tab_unselected" />
<item android:state_focused="false"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/tab_selected" />
<!-- Focused states -->
<item android:state_focused="true"
android:state_selected="false"
android:state_pressed="false"
android:drawable="@drawable/tab_focus" />
<item android:state_focused="true"
android:state_selected="true"
android:state_pressed="false"
android:drawable="@drawable/tab_focus" />
<!-- Pressed -->
<item android:state_pressed="true" android:drawable="@drawable/tab_press" />
</selector>