【发布时间】:2015-08-19 05:36:41
【问题描述】:
我有一个 Activity 和 android.support.v4.FragmentTabHost 我想测试它。
我的布局:
<LinearLayout
android:id="@+id/homeFragement"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/tabContent"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<View
android:layout_width="match_parent"
android:layout_height="1px"
android:background="@color/line_color" />
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<android.support.v4.app.FragmentTabHost
android:id="@+id/tabHost"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@color/bottom_bgcolor" />
</FrameLayout>
</LinearLayout>
这就是我初始化TabHost的方式:
mTabHost.setup(this, getSupportFragmentManager(), R.id.tabContent);
int count = 4;
for (int i = 0; i < count; i++) {
String title = getString(getTitleForIndex(i));
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(title);
tabSpec.setIndicator(createButtonView(i));
mTabHost.addTab(tabSpec, mFragments[i], null);
}
现在在我的测试用例中,test_fragment() 方法:
ActivityMonitor addMonitor = getInstrumentation().addMonitor(MainEntryActivity.class.getName(), null, false);
MainEntryActivity mainEntryActiviy = (MainEntryActivity) addMonitor.waitForActivityWithTimeout(3000);
android.support.v4.app.FragmentTabHost fragmentTabHost = (FragmentTabHost) mainEntryActiviy.findViewById(R.id.tabHost);
fragmentTabHost.setCurrentTab(4);
它可以工作,TabHost 已切换到选项卡 4。
如何获取TabHost 的内容,应该是Fragment。
【问题讨论】: