TabWidget에서 추가되는 Tab의 Background변경하기
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();
        }
    }

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>

저작자 표시 비영리 변경 금지
Posted by 유야

댓글을 달아 주세요

  1. 양은경 2010/05/19 19:03  댓글주소  수정/삭제  댓글쓰기

    안녕하세요 위 글 보다가 모르겠어서 질문드립니다.

    위에서 xml파일에서 사정없이 에러가 나는데요 ㅜㅜ;;;
    현재 버전(1.6 or 2.1)으로 하려면 Selector와 Item은 무엇으로 봐뀌는 건가요???

    • BlogIcon 유야 2010/05/20 22:31  댓글주소  수정/삭제

      아마도 이미지가 없으니까 에러가 나는거겠죠? ㅎㅎ
      android:drawable 에 적어놓은 이미지 파일들이 있어야 됩니다.. 위 코드는 그냥 예제인지라 ^^;;

    • 양은경 2010/05/24 11:04  댓글주소  수정/삭제

      아 이미지가 있어야 문제가 없는건가요??
      그럼 먼저 이미지를 만들고 해봐야 겠네요 ^^
      감사합니다.

  2. 임태훈 2010/06/08 16:22  댓글주소  수정/삭제  댓글쓰기

    좋은 글 감사합니다. 잘 보고 갑니다.
    근데 궁금한게 있는데요.
    제가 안드로이드 공부한지 얼마 되지 않아서 잘 모르는 부분이 많이 있네요.
    위 XML을 만들고
    setBackgroundDrawable에서 마지막에 getDrawable에
    R.drawable.tab_indicator이라고 되어 있는데
    위와 같이 xml으로 만들었을경우 보통은 Layout로 되어 있지 않나요??
    똑같이 R.drawable.tab_indicator이라고 하면 에러가 나는데..
    제가 뭐를 모르고 있는건지 잘 모르겠네요.

    • BlogIcon 유야 2010/06/08 22:05  댓글주소  수정/삭제

      drawable도 xml파일로 정의 할수 있습니다.
      tab_indicator.xml 을 drawable 폴더 아래에 넣으셔야합니다. 그래도 제대로 컴파일될려면 tab_indicator.xml안에서 사용한 tab_focus,tab_selected등으로 정의한 drawable이 있어야 겠지요..