Android开发之TabHost应用详解
2015-12-07 17:05
423 查看
TabHost的实现有两种方式:
第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。
第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。
1)继承TabActivity
res/layout/main.xml
HelloTabHost.java
2)不继承TabActivity
继承普通Activity,<TabWidget>标签id必须为tabs、<FrameLayout>标签id必须为tabcontent.这个方式在通过findViewById获得TabHost之后,必须要调用setup方法。
第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。
第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。
1)继承TabActivity
res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <!-- 定义TabHost组件 --> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- 定义第一个标签页的内容 --> <LinearLayout android:id="@+id/tab01" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- 定义两个TextView用于显示标签页中的内容 --> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="张三-2011/07/12"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="李四-2011/07/10"/> </LinearLayout> <!-- 定义第二个标签页的内容 --> <LinearLayout android:id="@+id/tab02" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="王五-2011/07/11"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="赵六-2011/07/10"/> </LinearLayout> <!-- 定义第三个标签页的内容 --> <LinearLayout android:id="@+id/tab03" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="王麻子-2011/07/12"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="小明-2011/07/08"/> </LinearLayout> </TabHost>
HelloTabHost.java
public class HelloTabHost extends TabActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //调用TabActivity的getTabHost()方法获取TabHost对象 TabHost tabHost = getTabHost(); //设置使用TabHost布局 LayoutInflater.from(this).inflate(R.layout.main, tabHost.getTabContentView(),true); //添加第一个标签页 tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("已接").setContent(R.id.tab01)); //添加第二个标签页,并在其标签上添加一个图片 tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("未接",getResources().getDrawable(R.drawable.icon)).setContent(R.id.tab02)); //添加第三个标签页 tabHost.addTab(tabHost.newTabSpec("tab03").setIndicator("通话记录").setContent(R.id.tab03)); } }
2)不继承TabActivity
继承普通Activity,<TabWidget>标签id必须为tabs、<FrameLayout>标签id必须为tabcontent.这个方式在通过findViewById获得TabHost之后,必须要调用setup方法。
main.xml代码
<?xml version="1.0" encoding="utf-8"?> <!-- TabHost必须包含一个 TabWidget和一个FrameLayout--> <TabHost android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="wrap_content"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- TabWidget的id属性必须为 @android:id/tabs--> <TabWidget android:id="@android:id/tabs" android:orientation="horizontal" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <!-- FrameLayout的id属性必须为 @android:id/tabcontent--> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/view1" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/view2" android:layout_width="fill_parent" android:layout_height="fill_parent"/> <TextView android:id="@+id/view3" android:layout_width="fill_parent" android:layout_height="fill_parent"/> </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>Java代码
public class TabHostTest extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); // 获取TabHost对象 TabHost tabHost = (TabHost) findViewById(R.id.tabhost); // 如果没有继承TabActivity时,通过该种方法加载启动tabHost tabHost.setup(); tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("first", getResources().getDrawable(R.drawable.icon)).setContent( R.id.view1)); tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("second") .setContent(R.id.view3)); tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("third") .setContent(R.id.view2)); } }
相关文章推荐
- Android开发中适配多种 ROM 的快捷方式是如何实现的?
- Android -wifi 直连(wifi direct )
- java Android 算一定业务规定的工作日(除去法定节假日、调休周末上班日)代码copy直接用
- android(38)(加载大图到内存)
- AndroidStudio中使用DDMS工具
- android 百度地图 在线建议查询
- Android蓝牙4.0读写数据返回状态码及出现异常解决办法
- Android中的MVP模式使用
- Android开发中适配多种 ROM 的快捷方式是如何实现的?
- android studio 重启adb
- Android Studio(as)调试总结
- android上传文件到服务器
- android获取控件宽高
- Jenkins构建Android项目持续集成之简介篇
- Android抽象布局——include、merge 、ViewStub
- android动画及用法
- android学习笔记10(2)——多个Activity跳转和传值
- Android控件之GridView自定义
- android(37)(绑定远程服务)
- Android通信汇总