如何更改 TabHost 的颜色?
How can I change the colors of 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
可以使用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
}
});
我想更改选项卡的颜色(激活的选项卡和未激活的选项卡)。我知道如何更改背景图片,但不知道如何更改选项卡的颜色:-(
我的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
可以使用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
}
});