2016-08-29 3 views
0

Я хочу изменить цвета вкладок (активированные вкладки и инактивированные вкладки). Я знаю, как я могу изменить фоновое изображение, но не так, как я могу изменить цвет вкладок :-(Как я могу изменить цвета TabHost?

Мой Java Код:

public class Inventar extends TabActivity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.inventar); 

    TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); 
    TabHost.TabSpec spec; 
    Intent intent; 

    spec = tabHost.newTabSpec("1"); 
    spec.setIndicator("Inventar 1"); 
    intent = new Intent(this, Spielregeln.class); 
    spec.setContent(intent); 
    tabHost.addTab(spec.setIndicator("",getResources().getDrawable(R.drawable.button))); 

    spec = tabHost.newTabSpec("2"); 
    spec.setIndicator("Inventar 2"); 
    intent = new Intent(this, Login.class); 
    spec.setContent(intent); 
    tabHost.addTab(spec.setIndicator("",getResources().getDrawable(R.drawable.button))); 

    spec = tabHost.newTabSpec("3"); 
    spec.setIndicator("Inventar 3"); 
    intent = new Intent(this, Registrieren.class); 
    spec.setContent(intent); 
    tabHost.addTab(spec.setIndicator("", getResources().getDrawable(R.drawable.tab_spec))); 


    tabHost.setCurrentTab(0); 
    tabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() { 
     @Override 
     public void onTabChanged(String tabId) { 

     } 
    }); 


} 

}

Мои XML код (tab_spec.xml):

<selector 
    xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item 
     android:state_selected="true" 
     android:drawable="@drawable/inventar" /> 
    <item 
     android:state_selected="false" 
     android:drawable="@drawable/freunde" /> 
</selector> 

Мой 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:orientation="vertical" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:padding="0dp"> 

     <TabWidget 
      android:id="@android:id/tabs" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" /> 
     <FrameLayout 
      android:id="@android:id/tabcontent" 
      android:layout_width="fill_parent" 
      android:layout_height="match_parent" 
      android:padding="5dp"> 

     </FrameLayout> 

    </LinearLayout> 
</TabHost> 
enter code here 

ответ

0

Вы можете установить с помощью onTabChangedListener,

tabHost.setOnTabChangedListener(new OnTabChangeListener() { 

     public void onTabChanged(String arg0) { 
      for (int i = 0; i < tab.getTabWidget().getChildCount(); i++) { 
       tabHost.getTabWidget().getChildAt(i) 
         .setBackgroundResource(R.drawable.tab_selected); // unselected 
      } 
      tabHost.getTabWidget().getChildAt(tab.getCurrentTab()) 
        .setBackgroundResource(R.drawable.tab_unselected); // selected 

     } 
    }); 
+0

Я получаю сообщение об ошибке: Не удается разрешить символ 'вкладку' – SilverBlue

+0

изменить его на объект TabHost .. –

+0

Спасибо! Он работает! – SilverBlue