我需要在不同的状态下将不同的图像设置为标签背景。我已将一个图像设置为默认背景,但在选择选项卡时如何切换到另一个图像。以下是我的代码。
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>
答案 0 :(得分:0)
实施onTabChangeListener()
并修改其背景。干杯
http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html
编辑: 使用tabHost实现该方法。您可以在任何地方实施。假设您设置了所有TabWidgets后,请执行此操作。使用标签的ID的好习惯就像你将它们设置为“第一”,“第二”等等。
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
}});
答案 1 :(得分:0)
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.
}
}
答案 2 :(得分:0)
这可能会对你有所帮助
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);
}});