cambiare lo sfondo Tab sulla selezione
Domanda
Ho bisogno di impostare diverse immagini come sfondo scheda su diversi stati. Ho impostato un'immagine come sfondo per impostazione predefinita, ma come passare ad altro, quando è selezionata la scheda. Qui di seguito è il mio codice.
public class HelloTabWidget extends TabActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
TabWidget tw = getTabWidget();
for (int i = 0; i < tw.getChildCount(); i++) {
View v = tw.getChildAt(i);
v.setBackgroundDrawable(getResources().getDrawable
(R.drawable.tab_artist));
}
//First tab
intent = new Intent().setClass(this, FirstActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("First").setIndicator("First")
.setContent(intent);
tabHost.addTab(spec);
getTabHost().getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabselected);
//Second tab
intent = new Intent().setClass(this, SecondActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Second").setIndicator("Second")
.setContent(intent);
tabHost.addTab(spec);
getTabHost().getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabselected);
//third
intent = new Intent().setClass(this, ThirdActivity.class); // Initialize a TabSpec for each tab and add it to the TabHost
spec = tabHost.newTabSpec("Third").setIndicator("Third")
.setContent(intent);
tabHost.addTab(spec);
getTabHost().getTabWidget().getChildAt(2).setBackgroundResource(R.drawable.tabselected);
}
}
/ * tab_artist.xml * /
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use grey -->
<item android:background="@drawable/tabselected" android:state_selected="true" />
<!-- When not selected, use white-->
<item android:background="@drawable/tabunselected" />
</selector>
Soluzione
public class HelloTabWidget extends TabActivity implements OnTabChangeListener{
.....
mTabHost. setOnTabChangedListener(this);
@Override
public void onTabChanged(String tabId) {
// Here in tabId you will get the name of the Tab from that you can check and set the background
// of the requirement tab according to need.
}
}
Altri suggerimenti
Implementare onTabChangeListener()
e ci modificare il loro background. Acclamazioni
http://developer.android.com/reference/android/ widget di / TabHost.OnTabChangeListener.html
Modifica: Utilizzare il tabHost per implementare il metodo. È possibile implementato dove vuoi. Diciamo che farlo dopo aver impostato tutti i TabWidgets. La sua buona pratica utilizzare gli ID della scheda come se li hai impostato "In primo luogo", "Secondo" ecc ecc.
tabHost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
if(tabId.equals("First"){
//do something
}else if(tabId.equals("Second"))
{
//do something
}// etc etc etc
}});
questo può aiutare
Tabhost.setOnTabChangedListener(new OnTabChangeListener(){
@Override
public void onTabChanged(String tabId) {
for(int i=0;i<tb.getTabWidget().getChildCount();i++)
{
tb.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.tabunselected); //unselected
}
tb.getTabWidget().getChildAt(tb.getCurrentTab()).setBackgroundResource(R.drawable.tabselected);
}});