刚开始学TabHost的使用,v4包的TabHost的简单用法
布局文件
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
代码
//1.初始化Tabhost
tabHost= (FragmentTabHost) findViewById(android.R.id.tabhost); //2.关联frameLayout
tabHost.setup(this,getSupportFragmentManager(),android.R.id.tabcontent);
//3.添加tab对应的fragment
TabHost.TabSpec spec = tabHost.newTabSpec("1");
spec.setIndicator("A");
tabHost.addTab(spec,BlankFragment.class,null);
如果有多个布局,则重复copy第三步
TabHost.TabSpec spec1 = tabHost.newTabSpec("2");
spec1.setIndicator("B");
tabHost.addTab(spec1,BlankFragment.class,null);
TabHost.TabSpec spec2 = tabHost.newTabSpec("3");
spec2.setIndicator("C");
tabHost.addTab(spec2,BlankFragment.class,null);