来源过于啰嗦,这里只有简化后的。

  转载请注明出处  http://www.cnblogs.com/zaiyuzhong/p/add-tab-dynamic-in-android.html

建立对应的布局配置:/res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<TabHost    android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/tabHost"
xmlns:android="http://schemas.android.com/apk/res/android"> <TabWidget
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@android:id/tabs"/> <FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@android:id/tabcontent"> </FrameLayout> </TabHost>
 1 import android.view.Menu;
2 import android.view.View;
3
4 import android.widget.AnalogClock;
5
6 import android.widget.TabHost;
7 import android.widget.TabHost.TabSpec;
8 import android.widget.TextView;
9
10
11 public class MainActivity extends FragmentActivity {
12
13 @Override
14 protected void onCreate(Bundle savedInstanceState) {
15 super.onCreate(savedInstanceState);
16 setContentView(R.layout.activity_main);
17
18 TabHost tabHost=(TabHost)findViewById(R.id.tabHost);
19 tabHost.setup();
20
21 TabSpec spec1=tabHost.newTabSpec("Tab1");
22 spec1.setContent(new TabHost.TabContentFactory() {
23 public View createTabContent(String tag) {
24 TextView txtView = new TextView(MainActivity.this);
25 txtView.setText("Tab Text in createTabContent");
26 return txtView;
27 }
28 });
29 spec1.setIndicator("Tab Text for setIndicator");
30
31
32 TabSpec spec2=tabHost.newTabSpec("Tab2");
33 spec2.setIndicator("Tab Clock");
34 spec2.setContent(new TabHost.TabContentFactory() {
35 public View createTabContent(String tag) {
36 return(new AnalogClock(MainActivity.this));
37 }
38 });
39 spec2.setIndicator("Clock");
40
41 tabHost.addTab(spec1);
42 tabHost.addTab(spec2);
43 }

代码

最新文章

  1. Linux计划任务.md
  2. SSH+JPA查询两个数据库
  3. Could not load file or assembly Microsoft.Web.Infrastructure
  4. 求助:为什么我用360浏览器和UC浏览器打不开JAVA中的index.html文件? 一打开就显示浏览器首界页
  5. 一些CSS技巧
  6. Cordova 3.0 Plugin 安装 及&quot;git&quot; command line tool is not installed
  7. kendo ui中grid页面参数问题
  8. QT连接mysql中文显示问题
  9. 主java程序猿知识体系结构
  10. [LeetCode] Complex Number Multiplication 复数相乘
  11. RIPng(第三组)
  12. C++面试集锦( 面试被问到的问题 )
  13. 计算机网络之互联网|因特网|万维网|HTTP|HTML之间的关系辨析
  14. wordpress安装主题时提示无法创建目录
  15. linux下的C++项目创建
  16. BFS &amp;&amp; DFS
  17. IPC介绍——10个ipcs例子
  18. Git 软件开发过程
  19. EJB学习手记
  20. spring boot 报错 Error creating bean with name

热门文章

  1. 真正“搞”懂HTTP协议12之缓存代理
  2. 浅谈Pytest中的marker
  3. TCP与UDP、socket模块
  4. 【OpenWrt】N1刷机过程及旁路由设置(通用)
  5. 样本熵(SampEn)的C/C++代码实现与优化
  6. 列表(list)内置方法补充、字典(dict)内置方法、元组(tuple)内置方法、集合(set)内置方法
  7. 单例 Bean 的线程安全问题
  8. kubernetes之Ingress发布Dashboard(二)
  9. nvm作用、下载、使用、常见问题
  10. Java第五讲异常处理总结