Android UI TabActivity问题
我正在尝试为该应用程序实现以下背景......
对于背景图像(应用程序背景)...我正在 setContentView(layout) 中设置图像。 ..通过添加这一行,我收到运行时异常...
如果我在子活动中设置此背景..我不会让背景填充完整的应用程序背景..知道还有什么替代方法吗?
public class HMITabActivity extends TabActivity{
private TabHost tabHost = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.background);
tabHost = getTabHost();
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
setTabHostColors();
}
});
tabHost.addTab(tabHost.newTabSpec("Tasks")
.setIndicator("Tasks", getResources().getDrawable(R.drawable.icon_task))
.setContent(new Intent(this, Tasks.class)));
tabHost.addTab(tabHost.newTabSpec("HMI")
.setIndicator("HMI", getResources().getDrawable(R.drawable.icon_hmi))
.setContent(new Intent(this, HMI.class)));
tabHost.addTab(tabHost.newTabSpec("Diagnostics")
.setIndicator("Diagnostics", getResources().getDrawable(R.drawable.icon_diagnostics))
.setContent(new Intent(this, Diagnostics.class)));
tabHost.addTab(tabHost.newTabSpec("About")
.setIndicator("About", getResources().getDrawable(R.drawable.icon_info))
.setContent(new Intent(this, Tasks.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
Intent intent = new Intent(BackgroundService.class.getName());
startService(intent);
}
private void setTabHostColors() {
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.rgb(1, 1, 1)); //unselected
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.rgb(50, 120, 160)); // selected
}
}
I am trying to implement the following background for the application...
For the background image(application background) ... i am setting the image in setContentView(layout)... by adding this line, i am getting a runtime exception...
if i set this background in the subactivities..i wont get the background to fill the full application background.. any idea whats the alternative?
public class HMITabActivity extends TabActivity{
private TabHost tabHost = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.background);
tabHost = getTabHost();
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
setTabHostColors();
}
});
tabHost.addTab(tabHost.newTabSpec("Tasks")
.setIndicator("Tasks", getResources().getDrawable(R.drawable.icon_task))
.setContent(new Intent(this, Tasks.class)));
tabHost.addTab(tabHost.newTabSpec("HMI")
.setIndicator("HMI", getResources().getDrawable(R.drawable.icon_hmi))
.setContent(new Intent(this, HMI.class)));
tabHost.addTab(tabHost.newTabSpec("Diagnostics")
.setIndicator("Diagnostics", getResources().getDrawable(R.drawable.icon_diagnostics))
.setContent(new Intent(this, Diagnostics.class)));
tabHost.addTab(tabHost.newTabSpec("About")
.setIndicator("About", getResources().getDrawable(R.drawable.icon_info))
.setContent(new Intent(this, Tasks.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
Intent intent = new Intent(BackgroundService.class.getName());
startService(intent);
}
private void setTabHostColors() {
for(int i=0;i<tabHost.getTabWidget().getChildCount();i++) {
tabHost.getTabWidget().getChildAt(i).setBackgroundColor(Color.rgb(1, 1, 1)); //unselected
}
tabHost.getTabWidget().getChildAt(tabHost.getCurrentTab()).setBackgroundColor(Color.rgb(50, 120, 160)); // selected
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为此,您必须使用自定义选项卡,这里是代码,请尝试以下操作:
其中prepareTabView是Inflate View的方法。
然后膨胀一个像这样的视图:
其中选项卡 XML 看起来像这样:
然后现在根据需要添加背景颜色。
For this you must use Custom Tabs ,here is the Code try this :
where prepareTabView is method that Inflate View.
Then Inflate a view like this :
Where tabs XML will look like this :
Then now add your backgroung color as you like..
TabActivity 从 Android 4.2、API 级别 17 开始已弃用。使用 Fragments 代替 TabActivity
TabActivity is Deprecated from Android 4.2,API level 17. Use Fragments instead of TabActivity