80,471
社区成员




TabHost myTabHost;
Context c;
private ActivityPage_101 activityOne;
private ActivityPage_102 activityTwo;
private ActivityPage_103 activityThree;
private ActivityPage_104 activityFour;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_tabhost);
c = this;
myTabHost = this.getTabHost();
initTabHost(c,myTabHost,
new TabHost.TabSpec[]{
myTabHost.newTabSpec("a").setIndicator(composeLayout("我的任务", R.drawable.tab_weixin_pressed)).setContent(new Intent(c,ActivityPage_101.class)),
myTabHost.newTabSpec("b").setIndicator(composeLayout("通讯录", R.drawable.tab_address_pressed)).setContent(new Intent(c,ActivityPage_102.class)),
myTabHost.newTabSpec("c").setIndicator(composeLayout("查询", R.drawable.tab_find_frd_pressed)).setContent(new Intent(c,ActivityPage_103.class)),
myTabHost.newTabSpec("d").setIndicator(composeLayout("我", R.drawable.tab_settings_pressed)).setContent(new Intent(c,ActivityPage_104.class))
});
initSubActivity();
}
public void initTabHost(final Context context,
final TabHost tabHost, TabSpec[] tabSpecs) {
for (TabSpec tabSpec : tabSpecs) {
tabHost.addTab(tabSpec);
}
final TabWidget tabWidget = tabHost.getTabWidget();
for (int i = 0; i < tabWidget.getChildCount(); i++) {
tabWidget.getChildAt(i).getLayoutParams().height = zoom(context,50);
}
for (int i = tabWidget.getChildCount() - 1; i >= 0; i--) {
tabHost.setCurrentTab(i);
}
}
private void initSubActivity()
{
try {
FrameLayout fl = myTabHost.getTabContentView();
for (int i1 = 0; i1 < fl.getChildCount(); i1++) {
View v = fl.getChildAt(i1);
Object a = v.getContext();
if (a instanceof ActivityPage_101) {
activityOne = (ActivityPage_101) a;
}
if (a instanceof ActivityPage_102) {
activityTwo = (ActivityPage_102) a;
}
if (a instanceof ActivityPage_103) {
activityThree = (ActivityPage_103) a;
}
if (a instanceof ActivityPage_104) {
activityFour = (ActivityPage_104) a;
}
}
} catch (Exception ex) {
//ActionTool.showException(c, ex);
}
}
<?xml version="1.0" encoding="utf-8"?>
<TabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<!-- 实现Tab标签的居底主要是通过设置属性 android:layout_weight="1" -->
<!-- 还要注意FrameLayout标签的位置,要写在TabWidget标签的前面 -->
<FrameLayout android:id="@android:id/tabcontent"
android:layout_weight="1" android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<TabWidget android:id="@android:id/tabs"
android:layout_alignParentBottom="true" android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</TabHost>