2016-08-29 2 views
0

Je souhaite modifier les couleurs des onglets (onglets activés et onglets désactivés). Je sais, comment je peux changer les images de fond, mais pas, comment je peux changer les couleurs des onglets :-(Comment changer les couleurs de TabHost?

Mon code 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) { 

     } 
    }); 


} 

}

Mon code 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> 

Mon XML-code (principal)

<?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 

Répondre

0

Vous pouvez régler à l'aide 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

Je reçois l'erreur: Impossible de résoudre le symbole 'onglet' – SilverBlue

+0

changer pour objet TabHost .. –

+0

Thank you! Ca court! – SilverBlue