2014-11-12 4 views
2

이미 선택되었을 때 하위 탭 색상을 변경할 수있는 탭 위젯이 있습니다. 하지만 이것은 안좋은 문제입니다. 안드로이드 탭 위젯의 테두리 선이 일관되게 잘 작동하지 않습니다. 때로는 다른 탭을 선택하여 명확하게하기 위해 테두리 선이 흰색으로 바뀌는 경우가 있습니다. 이것은 내 활동을위한 코드입니다. 여기 스택 오버 플로우에서 명성을 많이 얻었하지 않았기 때문에 나는 이미지를 제공 할 수없는 경우android tab host에서 선택한 탭의 색상을 변경하는 방법은 무엇입니까?

@SuppressWarnings("deprecation") 
public class TabHostActivity extends TabActivity { 

    private TabHost tabHost; 
    private int currentTab = 0; 
    private int lastTab = 0; 

    @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 

      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 

      setContentView(R.layout.activity_tabhost); 
      UserAccount userAccount = new UserAccount(); 

      tabHost = (TabHost) findViewById(android.R.id.tabhost); 


      TabSpec tab1 = tabHost.newTabSpec("First Tab"); 
      tab1.setContent(new Intent(this, HomeActivity.class)); 
      tab1.setIndicator("",getResources().getDrawable(R.drawable.home_icon)); 
      tabHost.addTab(tab1); 

      TabSpec tab2 = tabHost.newTabSpec("Second Tab"); 
      tab2.setContent(new Intent(this, About.class)); 
      tab2.setIndicator("",getResources().getDrawable(R.drawable.about_icon)); 
      tabHost.addTab(tab2); 


      TabSpec tab3 = tabHost.newTabSpec("Third Tab"); 
      tab3.setContent(new Intent(this, GridViewActivity.class)); 
      tab3.setIndicator("",getResources().getDrawable(R.drawable.gallery_icon)); 
      tabHost.addTab(tab3); 


      getTabHost().setOnTabChangedListener(new OnTabChangeListener() { 
       public void onTabChanged(String tabId) 
       { 
         currentTab = getTabHost().getCurrentTab(); 

         setCurrentTabColor(); 

         lastTab =currentTab; 
       } 
      }); 

      getTabWidget().getChildAt(lastTab).setBackgroundColor(Color.GRAY); 
     } 

     public void setCurrentTabColor(){ 
      getTabWidget().getChildAt(currentTab).setBackgroundColor(Color.GRAY); 
      getTabWidget().getChildAt(lastTab).setBackgroundColor(Color.parseColor("#FCAFA6")); 
     } 
} 

핑크 내 탭 위젯의 배경색을 설정 tabhost 내 XML은 임 죄송합니다
<?xml version="1.0" encoding="utf-8"?> 
<TabHost xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@android:id/tabhost" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <LinearLayout 
     android:id="@+id/LinearLayout01" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:orientation="vertical"> 

     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
      android:layout_weight="1" > 
     </FrameLayout> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="0" 
      android:background="#FCAFA6" > 
     </TabWidget> 
    </LinearLayout> 

</TabHost> 
+0

도움이되기를 바랍니다 변경할 때마다 호출 3 년 이상? – ianhanniballake

+0

내가 수업을 조각으로 만들지 않았기 때문에 – KirbyAbadilla

답변

0

첫째, 내 활동에, 내가 인스턴스 내 탭 호스트

tabHost = (FragmentTabHost) findViewById(R.id.tabContainer); 
tabHost.setup(this, getSupportFragmentManager(), R.id.tabContent); 
    tabHost.addTab(tabHost.newTabSpec("1").setIndicator("New Tab"), 
      PageFragment.class, null); 

나는이 선택되었는지 여부에 따라 내 탭의 색상을 업데이트하는 방법을 썼다

protected void updateTabs() { 



    for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++) { 

     if (tabHost.getTabWidget().getChildAt(i).isSelected()) { 
      SessionState.getSelectedIndex().setSelectedPage(i); 
      tabHost.getTabWidget() 
        .getChildAt(i) 
        .setBackgroundResource(
          R.drawable.tab_selected_background); 
     } else { 

      tabHost.getTabWidget() 
        .getChildAt(i) 
        .setBackgroundResource(
          R.drawable.tab_unselected_background); 

     } 
    } 

} 

는 그 방법을 탭이

tabhost.setOnTabChangedListener(new OnTabChangeListener() { 

     @Override 
     public void onTabChanged(String tabId) { 
      updateTabs(); 
     } 
    }); 

나는 이것이 당신이`TabActivity` 및 위해 사용되지 않습니다 다른 클래스를 사용하는 특별한 이유가