【问题标题】:Android TestCase how to get android.support.v4.FragmentTabHost's content viewAndroid TestCase如何获取android.support.v4.FragmentTabHost的内容视图
【发布时间】:2015-08-19 05:36:41
【问题描述】:

我有一个 Activityandroid.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

【问题讨论】:

    标签: android testcase


    【解决方案1】:

    用对应的xml布局文件创建一个从fragment扩展的新类,并在片段类的onCreateView中膨胀对应的布局:

    @Override
    public View onCreateView()
    {
         View view = inflator.inflate(R.id.your_layout_xml);
         return view;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-21
      • 1970-01-01
      • 2015-06-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多