2010-07-08 3 views
1

내 활동 중 하나에서 탭보기를 사용 중입니다. 탭의 드로어 블을 선택에 따라 변경하고 싶습니다. 그래서 그것은 - T11, T12, T21, T22의 4 개의 이미지가 있습니다. 먼저 탭 1을 선택하여 이미지 T11과 T22를 설정하고 싶습니다. 지금 나는 지금까지 내가 당김 유형의 XML 파일을 통해 사용하여 시도 최대한 빨리 탭 2.선택 항목의 탭 드로어 변경 - android

을 선택로 T12과 T21의 이미지를 변경하려면 : 왼쪽 탭 (TAB1)에 대한

당김 -

탭 권리를
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 

<item android:state_window_focused="false" android:state_enabled="true" 
    style="?attr/left_active" /> 

<item android:state_window_focused="false" android:state_enabled="false" 
    style="?attr/left_inactive" /> 
<item android:state_pressed="true" android:state_enabled="true" 
    style="?attr/left_active" /> 
<item android:state_pressed="true" android:state_enabled="false" 
    style="?attr/left_inactive" /> 
</selector> 

드로어 블 (TAB2) - 활동에서

<item android:state_window_focused="false" android:state_enabled="true" 
    style="?attr/right_active" /> 

<item android:state_window_focused="false" android:state_enabled="false" 
    style="?attr/right_inactive" /> 
<item android:state_pressed="true" android:state_enabled="true" 
    style="?attr/right_active" /> 
<item android:state_pressed="true" android:state_enabled="false" 
    style="?attr/right_inactive" /> 

:

TabHost tabHost = getTabHost(); 
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Tab1", getResources().getDrawable(R.drawable.left)).setContent(new Intent(this, Left.class))); 
tabHost.addTab(tabHost.newTabSpec("tab2") 
    .setIndicator("Tab2", getResources().getDrawable(R.drawable.right)) 
    .setContent(new Intent(this, Right.class))); 
tabHost.setCurrentTab(1); 

도와주세요 ...

+0

물음표 기호는 style =? – jonney

+0

@jonney 물음표는 현재 테마의 자원 값에 대한 참조임을 나타냅니다. – OrhanC1

답변

9

드디어 내 질문의 대답을 얻었다. 이전에 내가 한 일은 올바른 접근이었습니다. 내가 잘못하고있는 것은 스타일드로어 파일의 속성을 사용하고 있습니다. 탭의 배경 이미지로이 설정

드로어 블 파일 (이름 tab_left.xml와 당김 폴더에 파일을 생성)

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:state_focused="false" android:state_selected="false" 
     android:state_pressed="false" android:drawable="@drawable/tab_left_inactive" /> 

    <item android:state_focused="false" android:state_selected="true" 
     android:state_pressed="false" android:drawable="@drawable/tab_left_active" /> 

    <item android:state_focused="true" android:state_selected="false" 
     android:state_pressed="false" android:drawable="@drawable/tab_left_inactive" /> 

    <item android:state_focused="true" android:state_selected="true" 
     android:state_pressed="false" android:drawable="@drawable/tab_left_active" /> 

    <item android:state_pressed="true" 
     android:drawable="@drawable/tab_left_active" /> 
</selector> 

: 그래서 여기

는 예를 들어, 미래의 참조에 대한 코드입니다 :

TabWidget tw = getTabWidget(); 
View leftTabView = tw.getChildAt(0); 
leftTabView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_left); 
View rightTabView = tw.getChildAt(1); 
rightTabView.setBackgroundDrawable(getResources().getDrawable(R.drawable.tab_right); 
+0

정확히 내가 무엇을 찾고 있었는지, 매력처럼 작동하며 구현하기가 쉽습니다. 감사! – Emilio

+0

내 코드가 도움이 될 때 항상 낌새를 느낍니다 ... 도움이된다면 투표 버튼을 누를 수 있습니다. – mudit

+1

이미했습니다. :) – Emilio

1

저는 이것을 애플리케이션에서했지만 XML/스타일을 사용하지 않았습니다. 코드에서 수행 한 다음 onTabChanged() 메서드에서 배경 이미지를 교체합니다. 당신은 onTabChanged은 다음과 같을 것이다 포스트 Android TabHost - Activities within each tab

내 의견에 볼 수있는 코드의

부 :

public void onTabChanged(String tabId) { 
     if ("tabMap".equals(tabId)) { 
      txtTabMap.setBackgroundDrawable(getResources().getDrawable(newsList==null?R.drawable.bg_tab_right_active_left_inactive:R.drawable.bg_tab_middle_active_both_inactive)); 
      txtTabInfo.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_left_inactive_right_active)); 
      if(txtTabNews!=null)txtTabNews.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_right_inactive_left_active)); 
     } else if ("tabInfo".equals(tabId)) { 
      scrlDescription.scrollTo(0, 0); 
      txtTabMap.setBackgroundDrawable(getResources().getDrawable(newsList==null?R.drawable.bg_tab_right_inactive_left_active:R.drawable.bg_tab_middle_inactive_left_active)); 
      txtTabInfo.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_left_active_right_inactive)); 
      if(txtTabNews!=null)txtTabNews.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_right_inactive_left_inactive)); 
     } else if ("tabNews".equals(tabId)) { 
      txtTabMap.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_middle_inactive_right_active)); 
      txtTabInfo.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_left_inactive_right_inactive)); 
      if(txtTabNews!=null)txtTabNews.setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_tab_right_active_left_inactive)); 
     } 
    } 
+0

감사합니다. Mathias,하지만 다른 문제가 있습니다. 내 응용 프로그램에서 테마를 사용하고 있으므로 탭에 대해 둘 이상의 이미지가 표시되며 사용자가 응용 프로그램의 테마를 변경하면 변경됩니다. 그래서 내가 어떻게 구현합니까 ?? – mudit

관련 문제