当前位置: 首页 > news >正文

Tab与TabHost转

http://zxl-ong.iteye.com/blog/744809

 

第一种:继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。只要定义具体Tab内容布局就行了.

xml布局:

Java代码   收藏代码
  1. <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent" android:layout_height="fill_parent">  
  3.     <LinearLayout android:id="@+id/widget_layout_Blue"  
  4.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  5.         android:orientation="vertical" >  
  6.         <EditText android:id="@+id/widget34" android:layout_width="fill_parent"  
  7.             android:layout_height="wrap_content" android:text="EditText"  
  8.             android:textSize="18sp">  
  9.         </EditText>  
  10.         <Button android:id="@+id/widget30" android:layout_width="wrap_content"  
  11.             android:layout_height="wrap_content" android:text="Button">  
  12.         </Button>  
  13.     </LinearLayout>  
  14.     <LinearLayout android:id="@+id/widget_layout_red"  
  15.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  16.         android:orientation="vertical"  >  
  17.         <AnalogClock android:id="@+id/widget36"  
  18.             android:layout_width="wrap_content" android:layout_height="wrap_content">  
  19.         </AnalogClock>  
  20.     </LinearLayout>  
  21.     <LinearLayout android:id="@+id/widget_layout_green"  
  22.         android:layout_width="fill_parent" android:layout_height="fill_parent"  
  23.         android:orientation="vertical">  
  24.         <RadioGroup android:id="@+id/widget43"  
  25.             android:layout_width="166px" android:layout_height="98px"  
  26.             android:orientation="vertical">  
  27.             <RadioButton android:id="@+id/widget44"  
  28.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  29.                 android:text="RadioButton">  
  30.             </RadioButton>  
  31.             <RadioButton android:id="@+id/widget45"  
  32.                 android:layout_width="wrap_content" android:layout_height="wrap_content"  
  33.                 android:text="RadioButton">  
  34.             </RadioButton>  
  35.         </RadioGroup>  
  36.     </LinearLayout>  
  37. </FrameLayout>  
  38.   
  39. java代码:  
  40. super.onCreate(savedInstanceState);  
  41.         myTabhost=this.getTabHost();  
  42.         //get Tabhost  
  43.         LayoutInflater.from(this).inflate(R.layout.main, myTabhost.getTabContentView(), true);  
  44.         myTabhost.setBackgroundColor(Color.argb(1502270150));  
  45.           
  46.         myTabhost  
  47.                 .addTab(myTabhost.newTabSpec("One")// make a new Tab  
  48.                         .setIndicator("A")  
  49.                         // set the Title and Icon  
  50.                         .setContent(R.id.widget_layout_Blue));  
  51.         // set the layout  
  52.   
  53.         myTabhost  
  54.                 .addTab(myTabhost.newTabSpec("Two")// make a new Tab  
  55.                         .setIndicator("B",  
  56.                                 getResources().getDrawable(R.drawable.mumule))  
  57.                         // set the Title and Icon  
  58.                         .setContent(R.id.widget_layout_green));  
  59.         // set the layout  
  60.   
  61.         myTabhost  
  62.                 .addTab(myTabhost.newTabSpec("Three")// make a new Tab  
  63.                         .setIndicator("C",  
  64.                                 getResources().getDrawable(R.drawable.notepad))  
  65.                         // set the Title and Icon  
  66.                         .setContent(R.id.widget_layout_red));  



第二种:不用继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是
@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。TabHost的id可以自定义.

xml布局:

Java代码   收藏代码
    1. <?xml version="1.0" encoding="utf-8"?>  
    2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
    3.     android:id="@+id/hometabs"  
    4.     android:orientation="vertical"  
    5.     android:layout_width="fill_parent"    
    6.     android:layout_height="fill_parent">   
    7.     <TabHost android:id="@+id/tabhost"  
    8.         android:layout_width="wrap_content"  
    9.         android:layout_height="wrap_content">  
    10.         <LinearLayout  
    11.             android:orientation="vertical"  
    12.             android:layout_width="fill_parent"  
    13.             android:layout_height="fill_parent">  
    14.               
    15.             <TabWidget android:id="@android:id/tabs"   
    16.               android:orientation="horizontal"  
    17.               android:layout_width="wrap_content"  
    18.               android:layout_height="wrap_content">  
    19.             </TabWidget>  
    20.            
    21.              <FrameLayout android:id="@android:id/tabcontent"  
    22.                   android:layout_width="wrap_content"  
    23.                   android:layout_height="wrap_content">  
    24.                     <TextView android:id="@+id/view1"  
    25.                         android:layout_width="fill_parent"  
    26.                         android:layout_height="fill_parent"/>  
    27.                     <TextView android:id="@+id/view2"  
    28.                         android:layout_width="fill_parent"  
    29.                         android:layout_height="fill_parent"/>  
    30.                     <TextView android:id="@+id/view3"  
    31.                         android:layout_width="fill_parent"  
    32.                         android:layout_height="fill_parent"/>  
    33.              </FrameLayout>  
    34.            
    35.          </LinearLayout>  
    36.     </TabHost>  
    37. </LinearLayout>  
    38.   
    39. java代码:  
    40. protected void onCreate(Bundle savedInstanceState) {  
    41.         super.onCreate(savedInstanceState);  
    42.         setContentView(R.layout.hometabs);  
    43.           
    44.         TabHost tabHost = (TabHost) findViewById(R.id.tabhost);  
    45.         tabHost.setup();  
    46.         TabWidget tabWidget = tabHost.getTabWidget();  
    47.           
    48.         tabHost.addTab(tabHost.newTabSpec("tab1")  
    49.                 .setIndicator("tab1", getResources().getDrawable(R.drawable.mumule))  
    50.                 .setContent(R.id.view1));  
    51.           
    52.         tabHost.addTab(tabHost.newTabSpec("tab3")  
    53.                 .setIndicator("tab3")  
    54.                 .setContent(R.id.view3));  
    55.           
    56.         tabHost.addTab(tabHost.newTabSpec("tab2")  
    57.                 .setIndicator("tab2")  
    58.                 .setContent(R.id.view2));  
    59.           
    60.         final int tabs = tabWidget.getChildCount();  
    61.         Log.i(TAG, "***tabWidget.getChildCount() : " + tabs);  
    62.           
    63.         final int tabWidth = 90;  
    64.         final int tabHeight = 45;  
    65.           
    66.         for (int i = 0; i < tabs; i++) {  
    67.         /*  final View view = tabWidget.getChildAt(i); 
    68.             view.getLayoutParams().width = tabWidth; 
    69.             view.getLayoutParams().height = tabHeight; 
    70.             final TextView tv = (TextView) view.findViewById(android.R.id.title); 
    71.             tv.setTextColor(this.getResources().getColorStateList(android.R.color.black)); 
    72.             MarginLayoutParams tvMLP = (MarginLayoutParams)tv.getLayoutParams(); 
    73.             tvMLP.bottomMargin = 8;*/  
    74.         }  
    75.     } 

 

相关文章:

  • 路由器设置之五种接入方法
  • jConsole,jvisualvm和jmap使用
  • 创建索引
  • 总结概括对于大数据、高并发的网站如何进行优化的问题
  • NAT
  • javascript设计模式实践之代理模式--图片预加载
  • 数据库开发基本操作-Microsoft SQL Server Management Studio Express下载和安装
  • Extract+datapump+replicat 测试
  • Git 菜鸟变大神 (四)Github 创建远程仓库以及关联本地仓库
  • Flash移动开发的一本好书AndroidIOS
  • ::before和::after 常见的用法
  • 跟我一起学习C++虚函数--第二篇
  • MySQL优化order by导致的 using filesort
  • 管理日志-原创理论工具--技能方格图
  • objective-c系列-NSMutableString
  • “大数据应用场景”之隔壁老王(连载四)
  • javascript面向对象之创建对象
  • Java基本数据类型之Number
  • Laravel Mix运行时关于es2015报错解决方案
  • MySQL-事务管理(基础)
  • python_bomb----数据类型总结
  • SpiderData 2019年2月16日 DApp数据排行榜
  • tab.js分享及浏览器兼容性问题汇总
  • 测试如何在敏捷团队中工作?
  • 你真的知道 == 和 equals 的区别吗?
  • 实战|智能家居行业移动应用性能分析
  • 由插件封装引出的一丢丢思考
  • 带你开发类似Pokemon Go的AR游戏
  • 关于Android全面屏虚拟导航栏的适配总结
  • 选择阿里云数据库HBase版十大理由
  • #我与Java虚拟机的故事#连载02:“小蓝”陪伴的日日夜夜
  • (+4)2.2UML建模图
  • (¥1011)-(一千零一拾一元整)输出
  • (2024,Vision-LSTM,ViL,xLSTM,ViT,ViM,双向扫描)xLSTM 作为通用视觉骨干
  • (22)C#传智:复习,多态虚方法抽象类接口,静态类,String与StringBuilder,集合泛型List与Dictionary,文件类,结构与类的区别
  • (6)【Python/机器学习/深度学习】Machine-Learning模型与算法应用—使用Adaboost建模及工作环境下的数据分析整理
  • (二)Pytorch快速搭建神经网络模型实现气温预测回归(代码+详细注解)
  • (仿QQ聊天消息列表加载)wp7 listbox 列表项逐一加载的一种实现方式,以及加入渐显动画...
  • (一) storm的集群安装与配置
  • (原創) 如何動態建立二維陣列(多維陣列)? (.NET) (C#)
  • (转)一些感悟
  • ***linux下安装xampp,XAMPP目录结构(阿里云安装xampp)
  • *p++,*(p++),*++p,(*p)++区别?
  • .net分布式压力测试工具(Beetle.DT)
  • /etc/fstab 只读无法修改的解决办法
  • /proc/vmstat 详解
  • @kafkalistener消费不到消息_消息队列对战之RabbitMq 大战 kafka
  • @SentinelResource详解
  • [ 数据结构 - C++]红黑树RBTree
  • [Arduino学习] ESP8266读取DHT11数字温湿度传感器数据
  • [BZOJ1053][HAOI2007]反素数ant
  • [bzoj1912]异象石(set)
  • [C/C++]数据结构 循环队列
  • [C][栈帧]详细讲解
  • [Day 8] 區塊鏈與人工智能的聯動應用:理論、技術與實踐