【发布时间】:2012-04-23 19:54:46
【问题描述】:
我在标签栏中有四个标签。每当我启动我的应用程序时,我都需要运行选项卡栏的所有选项卡(四个),因为所有选项卡都包含应在启动应用程序时加载的 web 视图。但是第一个选项卡应该突出显示。我用过
setCurrentTab(0);
加载第一个选项卡,但我无法在不单击的情况下加载剩余的选项卡。
【问题讨论】:
我在标签栏中有四个标签。每当我启动我的应用程序时,我都需要运行选项卡栏的所有选项卡(四个),因为所有选项卡都包含应在启动应用程序时加载的 web 视图。但是第一个选项卡应该突出显示。我用过
setCurrentTab(0);
加载第一个选项卡,但我无法在不单击的情况下加载剩余的选项卡。
【问题讨论】:
为此,首先创建一个 xml 文件。示例文件如下所示。
<?xml version="1.0" encoding="utf-8"?>
<TabHost android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@android:id/tabhost"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="@drawable/background">
<TabWidget android:layout_width="fill_parent" android:id="@android:id/tabs"
android:layout_height="wrap_content" />
<FrameLayout android:layout_width="fill_parent" android:id="@android:id/tabcontent"
android:layout_gravity="bottom" android:paddingTop="70dp"
android:layout_height="match_parent">
<ScrollView android:layout_height="fill_parent" android:id="@+id/viewTab1"
android:layout_width="match_parent">
<!-- Your view you want -->
</ScrollView>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="@+id/viewTab2">
<!-- Your view you want -->
</LinearLayout>
<!-- Add as many view you want with unique id -->
</FrameLayout>
每个布局都将引用一个独特的布局。
在主要活动中使用以下
TabHost tabHost = getTabHost();
TabSpec spec1 = tabHost.newTabSpec("FirstTabView");
TabSpec spec2 = tabHost.newTabSpec("SecondTabView");
spec1.setContent(R.id.viewTab1);
spec1.setIndicator("First Tab");
spec2.setIndicator("Second Tab");
spec2.setContent(R.id.viewTab2);
tabHost.addTab(spec1);
tabHost.addTab(spec2);
tabHost.setup();
您可以根据需要添加任意数量的选项卡。而这一切都会同时被初始化。
希望对你有帮助:)
【讨论】: