Android---61---TabHost简单使用
2015-06-27 17:14
561 查看
与TabHost结合使用的组件:
TabWidget:代表选项卡的标签条
TabSpec:代表选项卡的一个Tab页面
TabHost仅仅是一个简单的容器,它提供两个方法来创建、添加选项卡
newTabSpec(String tag):创建选项卡
addTab(TabHOst.TabSpec tabSpec):添加选项卡
步骤:
1.在界面布局文件中定义TabHost组件,并为该组件定义该选项卡的内容
2.Activity继承TabActivity
3.调用TAbActivity的getTabHost()方法获取TabHost对象
4.通过TabHost对象的方法来创建、添加选项卡
布局文件:
TabHost容器内部需要组合两个组件:TabWidget和FrameLayout,其中TabWidget定义选项卡的标题条
Framelayout则用于层叠组合多个选项界面
Activity:
有时候选项卡是在下边的,只需要修改布局文件即可
布局文件:
TabWidget:代表选项卡的标签条
TabSpec:代表选项卡的一个Tab页面
TabHost仅仅是一个简单的容器,它提供两个方法来创建、添加选项卡
newTabSpec(String tag):创建选项卡
addTab(TabHOst.TabSpec tabSpec):添加选项卡
步骤:
1.在界面布局文件中定义TabHost组件,并为该组件定义该选项卡的内容
2.Activity继承TabActivity
3.调用TAbActivity的getTabHost()方法获取TabHost对象
4.通过TabHost对象的方法来创建、添加选项卡
布局文件:
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- 定义第一个标签页的内容 --> <LinearLayout android:id="@+id/tab01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tab1" android:textSize="11pt" /> </LinearLayout> <!-- 定义第二个标签页的内容 --> <LinearLayout android:id="@+id/tab02" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tab2" android:textSize="11pt" /> </LinearLayout> <!-- 定义第三个标签页的内容 --> <LinearLayout android:id="@+id/tab03" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:textSize="11pt" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tab3" android:textSize="11pt" /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost>
TabHost容器内部需要组合两个组件:TabWidget和FrameLayout,其中TabWidget定义选项卡的标题条
Framelayout则用于层叠组合多个选项界面
Activity:
public class MainActivity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // 获取该Activity里面的TabHost组件 TabHost tabHost = getTabHost(); // 创建第一个Tab页 TabSpec tab1 = tabHost.newTabSpec("tab1").setIndicator("01") // 设置标题 .setContent(R.id.tab01); // 设置内容 // 添加第一个标签页 tabHost.addTab(tab1); TabSpec tab2 = tabHost .newTabSpec("tab2") // 在标签标题上放置图标 .setIndicator("02", getResources().getDrawable(R.drawable.ic_launcher)) .setContent(R.id.tab02); // 添加第二个标签页 tabHost.addTab(tab2); TabSpec tab3 = tabHost.newTabSpec("tab3").setIndicator("03") .setContent(R.id.tab03); // 添加第三个标签页 tabHost.addTab(tab3); } }
有时候选项卡是在下边的,只需要修改布局文件即可
布局文件:
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_weight="1" > <RelativeLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@android:id/tabs" > <!-- 定义第一个标签页的内容 --> <LinearLayout android:id="@+id/tab01" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tab1" android:textSize="11pt" /> </LinearLayout> <!-- 定义第二个标签页的内容 --> <LinearLayout android:id="@+id/tab02" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tab2" android:textSize="11pt" /> </LinearLayout> <!-- 定义第三个标签页的内容 --> <LinearLayout android:id="@+id/tab03" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" android:textSize="11pt" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="tab3" android:textSize="11pt" /> </LinearLayout> </FrameLayout> </RelativeLayout> </TabHost>
相关文章推荐
- Android中数据库的操作流程详细解释
- Android Xposed框架出现java.lang.IllegalAccessError: Class ref in pre-verified class resolved to unexpected implementation问题
- Android高通平台调试Camera驱动全纪录
- Android高通平台调试Camera驱动全纪录
- 高通 android平台LCD驱动分析
- 高通 android平台LCD驱动分析
- android JBOX2D粒子碰撞的实例,以达到特殊效果
- 在android中做一个竖着的seekbar效果
- Android Dialog使用举例
- 国内如何下载 Android source code
- Android APK 签名比对详解
- Android中摇一摇效果的实现
- android 开源框架Universal-Image-Loader解析
- Android Chronometer(定时器)
- android快速开发--常用utils类
- Android实现ButterKnife
- Android屏幕适配全攻略(最权威的官方适配指导)
- android 倒计时控件
- Android按钮点击监听事件的写法
- android studio打包APK