Android之TabHost布局(转)
2015-07-09 15:46
459 查看
1.概念
盛放Tab的容器就是TabHost。TabHost的实现有两种方式:
第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。
第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。
2.案例
1)继承TabActivity
res/layout/main.xml
HelloTabHost.java
运行效果:
2)不继承TabActivity
继承普通Activity,<TabWidget>标签id必须为tabs、<FrameLayout>标签id必须为tabcontent.这个方式在通过findViewById获得TabHost之后,必须要调用setup方法。
Java代码
效果图
转自:链接
盛放Tab的容器就是TabHost。TabHost的实现有两种方式:
第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。
第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。
2.案例
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("第一个标签", getResources().getDrawable(R.drawable.icon)).setContent( R.id.view1)); tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("第三个标签") .setContent(R.id.view3)); tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("第二个标签") .setContent(R.id.view2)); } }
效果图
转自:链接
相关文章推荐
- Android Studio插件之sexy editor(设置AS背景)
- Android手机定位技术的发展
- Android View 仿iOS SwitchButton
- Android Sdk 国内镜像下载地址
- android:textSize="20dp" 中20dp是什么意思?
- android:ToolBar详解(手把手教程)
- android系统相机的使用、及解决拍照闪退的问题
- 【第三篇】学习 android 事件总线androidEventbus之list数据事件的传递,发送list数据事件到另外一个Activity
- Android 一种截图方法,稍微修改.保存
- Android Studio 工程.GitIgnore应该忽略的文件
- Android 当打开“开发者模式”中的“不保留活动”后,程序应当怎么保持正常运行
- Android AOSP输入法(LatinIME)大写判断分析
- android 动画总结笔记 一
- 获取Android界面信息
- Android设置拍照或者上传本地图片
- android获取mp4视频文件总时长和视频宽高<转>
- Android Parcelable vs Serializable
- AndroidStudio使用本地aar包
- EventBus使用详解
- Android Maven pom.xml