본문 바로가기

개발도구/aOS - 안드로이드 개발

[안드로이드] tabHost 와 tab버튼 이미지 변경

protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
       
       
        setContentView(R.layout.host);               

        tabHost = getTabHost();
        tabHost.setOnTabChangedListener(this);

        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("",getResources().getDrawable(R.drawable.tab_menu1_off))
                .setContent(new Intent(this, PhotoSwitcher.class)));
      
        tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator("",getResources().getDrawable(R.drawable.tab_menu2_off))               
                .setContent(new Intent(this, PhotoSwitcher.class)));
       
        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("",getResources().getDrawable(R.drawable.tab_menu3_off))
                .setContent(new Intent(this, PhotoSwitcher.class)));

        tabHost.addTab(tabHost.newTabSpec("tab4")
                .setIndicator("",getResources().getDrawable(R.drawable.tab_menu4_off))
            //   .setIndicator("스타",getResources().getDrawable(R.drawable.tab_menu4_off)) - 택스트 넣기
                .setContent(new Intent(this, PhotoSwitcher.class)));

tabHost.setCurrentTab(0); - 탭 1 상태 초기
       
    }

    @Override
    public void onTabChanged(String tabId) {
        // TODO Auto-generated method stub
//  배경넣기//tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.parseColor("#7392B5"));
  
// 클릭할때 이미지 변경    
        int menu_off[] ={R.drawable.tab_menu1_off,R.drawable.tab_menu2_off,R.drawable.tab_menu3_off,R.drawable.tab_menu4_off};
        int menu_on[] = {R.drawable.tab_menu1_on,R.drawable.tab_menu2_on,R.drawable.tab_menu3_on,R.drawable.tab_menu4_on};
       
        for(int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) {
            iv = (ImageView)tabHost.getTabWidget().getChildAt(i).findViewById(android.R.id.icon);
            iv.setImageDrawable(getResources().getDrawable(menu_off[i]));
        }
            ip = (ImageView)tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).findViewById(android.R.id.icon);
            ip.setImageDrawable(getResources().getDrawable(menu_on[tabHost.getCurrentTab()]));
           

    }