TabHost的xml文件必须是要以FrameLayout为主,例如下面是三个分页的文本视图:
java代码如下:
TabHost tabHost = this.getTabHost();
LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(),
true);
tabHost.addTab(tabHost.newTabSpec("Tab_1").setContent
(R.id.tab1).setIndicator("添加",this.getResources().getDrawable
(R.drawable.ic_launcher)));
tabHost.addTab(tabHost.newTabSpec("Tab_2").setContent
(R.id.tab2).setIndicator("修改",this.getResources().getDrawable
(R.drawable.ic_launcher)));
tabHost.addTab(tabHost.newTabSpec("Tab_3").setContent
(R.id.tab3).setIndicator("删除",this.getResources().getDrawable
(R.drawable.ic_launcher)));
//分页视图头,
tabHost.setCurrentTab(1);//初始化为第二页,其index从0开始
tabHost.setOnTabChangedListener(new OnTabChangeListener() {
@Override
public void onTabChanged(String tabId) {
// TODO Auto-generated method stub;
AlertDialog dia=new AlertDialog.Builder(Tabactivity.this).setTitle("提示").setMessage("选 中了" + tabId +"选项卡").setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.cancel();
}
}).create();
dia.show();
}
});
android中tabHost设置标题的颜色和大小
TabWidget tabWidget = this.getTabWidget();//TabHost中有一个getTabWidget()的方法
for (int i = 0; i < tabWidget.getChildCount(); ++i){
TextView tv = (TextView)tabWidget.getChildAt(i).findViewById(android.R.id.title);
tv.setGravity(BIND_AUTO_CREATE);
tv.setPadding(10, 10,10, 10);
tv.setTextSize(16);//设置字体的大小;
tv.setTextColor(Color.WHITE);//设置字体的颜色;
//获取tabs图片;
ImageView iv=(ImageView)tabWidget.getChildAt(i).findViewById(android.R.id.icon);
}
//设置tabHost的高度
for (int i = 0; i < tabWidget.getChildCount(); i++) {
View view = tabWidget.getChildTabViewAt(i);
view.getLayoutParams().height = 80; //tabWidget.getChildAt(i)
final TextView tv = (TextView) view.findViewById(android.R.id.title);
tv.setTextSize(28);
tv.setTextColor(this.getResources().getColorStateList(
android.R.color.white));