UI组件:选项卡(TabHost)
来源:互联网 发布:淘宝落地窗帘布 编辑:程序博客网 时间:2024/05/16 10:02
与TabHost结合使用的还有如下组件:
- TabWidget : 代表选项卡的标签条。
- TabSpec : 代表选项卡的一个Tab页面。
使用TabHost的一般步骤如下:
- 在界面布局文件中定义TabHost组件,并为该组件定义该选项卡的内容。
- Activity应该继承TabActivity。
- 调用TabActivity的getTabHost()方法获取TabHost对象。
- 通过TabHost对象的方法来创建、添加选项卡。
TabHost只是一个容器,它提供了如下两个方法来创建、添加选项卡:
- newTabSpec(String tab) : 创建选项卡。
- addTab(TabHost.TabSpec tabSpec) : 添加选项卡。
除此之外,TabHost还提供了一些方法来获取当前选项卡,获取当前View的方法,具体可以参考API文档。
下面的实例实现的通话记录界面。
界面效果如下图所示:
布局文件代码如下:
<?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"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <!--TabWidget定义选项卡的标题条--> <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" android:layout_margin="10dp"> <!--定义第一个标签页的内容--> <LinearLayout android:id="@+id/tab1" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="tab1"/> </LinearLayout> <!--定义第二个标签页的内容--> <LinearLayout android:id="@+id/tab2" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="tab2"/> </LinearLayout> <!--定义第三个标签页的内容--> <LinearLayout android:id="@+id/tab3" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="tab3"/> </LinearLayout> </FrameLayout> </LinearLayout></TabHost>
主程序代码如下所示:
public class TabHostActivity extends TabActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tab_host); TabHost tabHost = getTabHost(); //创建第一个Tab页 TabHost.TabSpec tab1 = tabHost.newTabSpec("tab1") .setIndicator("已接电话") //设置标题 .setContent(R.id.tab1); //设置内容 //添加第一个标签页 tabHost.addTab(tab1); TabHost.TabSpec tab2 = tabHost.newTabSpec("tab2") .setIndicator("呼出电话") .setContent(R.id.tab2); //添加第二个标签页 tabHost.addTab(tab2); TabHost.TabSpec tab3 = tabHost.newTabSpec("tab3") .setIndicator("未接电话") .setContent(R.id.tab3); //添加第三个标签页 tabHost.addTab(tab3); }}
0 0
- UI组件:选项卡(TabHost)
- Android的UI组件之TabHost(选项卡)的功能和用法
- 选项卡(TabHost)
- android学习--TabHost选项卡组件
- 各种杂项组件(4)之----SearchView(搜索框)、TabHost(选项卡)、ScrollView(滚动视图)
- [阶段二]Android UI选项卡组件
- 选项卡TabHost
- [控件]TabHost选项卡
- 初识选项卡 TabHost
- 选项卡 TabHost
- TabHost...选项卡
- android 选项卡TabHost
- android 选项卡TabHost
- TabHost 选项卡
- TabHost--选项卡案例
- tabhost选项卡,做微信以及微博UI界面必学
- android 选项卡(TabHost) 置底
- android 底部选项卡(TabHost)
- HTML+CSS之CSS盒模型
- 让ssh从stdin读取密码
- js-prototype函数
- (原创)安卓传感器入门介绍
- 一起学opencv (八) 分水岭和选取segment
- UI组件:选项卡(TabHost)
- |洛谷|贪心|P1376 机器工厂
- 异型卵问题
- 排序法之冒泡排序法
- ScrollView嵌套listView
- 各种滤波算法的比较
- 安卓适配之终极方案,百分比适配
- arguments的说明
- 导入环信Demo之'Execution failed for task 'apptransformClassesWithJarMergingForDebug'