TabHost 也就相当于Windows下的选项框
有两种实现方式
1. 继承TabActivity (已经废弃):从TabActivity中用getTabHost()方法获取TabHost
2. 在布局文件中定义TabHost,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent.
主要介绍第二种方法的使用
Layout
MainActivity
@ Override protected void onCreate ( Bundle savedInstanceState ) { super.onCreate ( savedInstanceState ); setContentView ( R.layout.activity_main ); TabHost tabHost = ( TabHost ) findViewById ( R.id.tabhost ); /** * 如何继承TabActivity 则不需要回调该函数 */ tabHost.setup ( ); /** * 增加标题头文件 ,相当于选项框 */ tabHost.addTab ( tabHost.newTabSpec ( "tab1" ) .setIndicator ( "文本新闻" , getResources ( ).getDrawable ( R.drawable.ic_launcher ) ) .setContent ( R.id.text ) ); //添加选项框二 tabHost.addTab ( tabHost.newTabSpec ( "tab2" ) .setIndicator ( "图片新闻" , getResources ( ).getDrawable ( R.drawable.ic_launcher ) ) .setContent ( R.id.photo ) ); //添加选项框三 tabHost.addTab ( tabHost.newTabSpec ( "tab3" ) .setIndicator ( "视频新闻" , getResources ( ).getDrawable ( R.drawable.ic_launcher ) ) .setContent ( R.id.video ) ); }