abHost mTabHost;
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState)
{
super
.onCreate(savedInstanceState);
setContentView(R.layout.main);
mTabHost = getTabHost();
mTabHost.addTab(mTabHost.newTabSpec(
"tab_1"
)
.setIndicator(
"TAB 1"
,getResources().getDrawable(R.drawable.img1))
.setContent(R.id.textview1));
mTabHost.addTab(mTabHost.newTabSpec(
"tab_2"
)
.setIndicator(
"TAB 2"
,getResources().getDrawable(R.drawable.img2))
.setContent(R.id.textview2));
mTabHost.addTab(mTabHost.newTabSpec(
"tab_3"
)
.setIndicator(
"TAB 3"
,getResources().getDrawable(R.drawable.img3))
.setContent(R.id.textview3));
mTabHost.setBackgroundColor(Color.argb(
150
,
22
,
70
,
150
));
mTabHost.setBackgroundResource(R.drawable.bg0);
mTabHost.setCurrentTab(
0
);
mTabHost.setOnTabChangedListener(
new
OnTabChangeListener()
{
@Override
public
void
onTabChanged(String tabId)
{
}
});
}
}
转入核心问题吧。
1
.将TabHost的标题的图标改变吧。
首先我们应该获取到布局View,再获取对应的控件,图标应该是ImageView,文本应该是TextView。
代码如下:
View mView = mTabHost.getTabWidget().getChildAt(
0
);
ImageView imageView = (ImageView)mView.findViewById(Android.R.id.icon);
imageView .setImageDrawable(getResources().getDrawable(R.drawable.img3));
imageView = (ImageView)mTabHost.getTabWidget().getChildAt(
1
).findViewById(Android.R.id.icon);
imageView .setImageDrawable(getResources().getDrawable(R.drawable.img2));
imageView = (ImageView)mTabHost.getTabWidget().getChildAt(
2
).findViewById(Android.R.id.icon);
imageView .setImageDrawable(getResources().getDrawable(R.drawable.img1));
2
.将TabHost的标题改变吧。
TextView textview = (TextView)mTabHost.getTabWidget().getChildAt(
0
).findViewById(Android.R.id.title);
textview.setTextSize(
1
);
textview = (TextView)mTabHost.getTabWidget().getChildAt(
1
).findViewById(Android.R.id.title);
textview.setTextSize(
2
);
textview = (TextView)mTabHost.getTabWidget().getChildAt(
2
).findViewById(Android.R.id.title);
textview.setTextSize(
3
);
其实获取了View,我们还可以再此基础上可以设置标题的颜色啊,字体啊,字体大小等等哦。