选择时选项卡背景发生变化
我需要在不同状态下设置不同的图像作为选项卡背景。我已将一张图像设置为默认背景,但如何在选择选项卡时切换到另一张图像。下面是我的代码。
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>
I need to set different Images as tab background on different states. I have set one image as background for default but how to switch to other one when tab is selected. Below is my code.
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
实现 onTabChangeListener() 并修改它们的背景。干杯
http://developer.android.com/reference/android/widget/ TabHost.OnTabChangeListener.html
编辑:
使用 tabHost 来实现该方法。您可以在任何您想要的地方实施。假设在设置所有 TabWidget 后执行此操作。最好的做法是使用选项卡的 id,就像将它们设置为“第一”、“第二”等。
Implement
onTabChangeListener()
and there modify their backgrounds. Cheershttp://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html
Edit:
Use the tabHost to implement the method. You can implemented where ever you want. Let's say do it after you set all the TabWidgets. Its good practice to use ids of the tab like you've set them "First", "Second" etc etc.
这可能对你有帮助
this may help you