Loading [MathJax]/jax/input/TeX/config.js
前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >专栏 >选项卡TabHost

选项卡TabHost

作者头像
欢醉
发布于 2018-01-22 02:50:50
发布于 2018-01-22 02:50:50
1.6K00
代码可运行
举报
文章被收录于专栏:james大数据架构james大数据架构
运行总次数:0
代码可运行

1.布局

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 1 <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:id="@+id/tabHost"
 4     android:layout_width="match_parent"
 5     android:layout_height="match_parent" >
 6 
 7     <LinearLayout
 8         android:id="@+id/tab1"
 9         android:layout_width="match_parent"
10         android:layout_height="match_parent"
11         android:orientation="vertical"
12         tools:context=".AndroidTabHostActivity" >
13 
14         <TextView
15             android:layout_width="wrap_content"
16             android:layout_height="wrap_content"
17             android:layout_centerHorizontal="true"
18             android:layout_centerVertical="true"
19             android:text="第一个" />
20     </LinearLayout>
21 
22     <LinearLayout
23         android:id="@+id/tab2"
24         android:layout_width="match_parent"
25         android:layout_height="match_parent"
26         android:orientation="vertical"
27         tools:context=".AndroidTabHostActivity" >
28 
29         <TextView
30             android:layout_width="wrap_content"
31             android:layout_height="wrap_content"
32             android:layout_centerHorizontal="true"
33             android:layout_centerVertical="true"
34             android:text="第二个" />
35     </LinearLayout>
36 
37     <LinearLayout
38         android:id="@+id/tab3"
39         android:layout_width="match_parent"
40         android:layout_height="match_parent"
41         android:orientation="vertical"
42         tools:context=".AndroidTabHostActivity" >
43 
44         <TextView
45             android:layout_width="wrap_content"
46             android:layout_height="wrap_content"
47             android:layout_centerHorizontal="true"
48             android:layout_centerVertical="true"
49             android:text="第三个" />
50     </LinearLayout>
51 
52 </TabHost>

2.逻辑控制

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 1 package com.example.androidtabhost;
 2 
 3 import android.os.Bundle;
 4 import android.app.Activity;
 5 import android.app.TabActivity;
 6 import android.view.LayoutInflater;
 7 import android.view.Menu;
 8 import android.widget.TabHost;
 9 
10 public class AndroidTabHostActivity extends TabActivity {
11 //注意:extends TabActivity
12     @Override
13     protected void onCreate(Bundle savedInstanceState) {
14         super.onCreate(savedInstanceState);
15         //setContentView(R.layout.activity_android_tab_host);
16         TabHost tabHost=getTabHost();//(TabHost)this.findViewById(R.id.tabHost);
17         //设置TabHost布局
18         LayoutInflater.from(this).inflate(R.layout.activity_android_tab_host,tabHost.getTabContentView(),true);
19         //添加第一个标签页
20         tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("已接电话").setContent(R.id.tab1));
21         //添加第2个标签页
22         tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("呼出电话",getResources().getDrawable(R.drawable.sj)).setContent(R.id.tab2));        
23         //添加第3个标签页
24         tabHost.addTab(tabHost.newTabSpec("tab03").setIndicator("未接电话").setContent(R.id.tab3));
25         
26     }
27 
28     @Override
29     public boolean onCreateOptionsMenu(Menu menu) {
30         // Inflate the menu; this adds items to the action bar if it is present.
31         getMenuInflater().inflate(R.menu.activity_android_tab_host, menu);
32         return true;
33     }
34 
35 }

TabHost是整个Tab的容器,包括两部分,TabWidget和FrameLayout。TabWidget就是每个tab的标签,FrameLayout则是tab内容

TabHost的二种实现方式: 第一种:继承TabActivity

1、如果我们使用extendsTabAcitivty,如同ListActivity,TabHost必须设置为@android:id/tabhost 2、TabWidget必须设置android:id为@android:id/tabs 3、FrameLayout需要设置android:id为@android:id/tabcontent

第二种:只是单纯的继承Activity类

布局文件  和上面一样 只是TabHost 的id 换为

tabHost = (TabHost)findViewById(R.id.m_tabhost);       //如果通过findViewById得到TabHost一定要调用 TabHost.setup();       LocalActivityManagerlocalAcManager = new LocalActivityManager(MainActivity.this,true);       localAcManager.dispatchCreate(savedInstanceState); tabHost.setup(localAcManager);

还可以自定义标签,将TabWidget android:visibility="gone" 然后自己弄些控件替代

另一实现自定义底部菜单

布局文件

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 1 <?xml version="1.0" encoding="UTF-8"?>  
 2 <TabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"  
 3   xmlns:android="http://schemas.android.com/apk/res/android">  
 4     <LinearLayout    
 5         android:orientation="vertical"    
 6         android:layout_width="fill_parent"    
 7         android:layout_height="fill_parent">  
 8         <FrameLayout    
 9             android:id="@android:id/tabcontent"    
10             android:layout_width="fill_parent"    
11             android:layout_height="0.0dip"    
12             android:layout_weight="1.0" />  
13         <TabWidget    
14             android:id="@android:id/tabs"    
15             android:visibility="gone"    
16             android:layout_width="fill_parent"    
17             android:layout_height="wrap_content"    
18             android:layout_weight="0.0" />  
19         <RadioGroup    
20             android:gravity="center_vertical"    
21             android:layout_gravity="bottom"    
22             android:orientation="horizontal"    
23             android:id="@+id/main_radio"    
24             android:background="@drawable/maintab_toolbar_bg"    
25             android:layout_width="fill_parent"    
26             android:layout_height="wrap_content">  
27             <RadioButton    
28                 android:id="@+id/radio_button0"    
29                 android:tag="radio_button0"    
30                 android:layout_marginTop="2.0dip"    
31                 android:text="@string/alarm"    
32                 android:drawableTop="@drawable/icon_1"    
33                 style="@style/main_tab_bottom" />  
34             <RadioButton    
35                 android:id="@+id/radio_button1"    
36                 android:tag="radio_button1"    
37                 android:layout_marginTop="2.0dip"    
38                 android:text="@string/message"    
39                 android:drawableTop="@drawable/icon_2"    
40                 style="@style/main_tab_bottom" />  
41             <RadioButton    
42                 android:id="@+id/radio_button2"    
43                 android:tag="radio_button2"    
44                 android:layout_marginTop="2.0dip"    
45                 android:text="@string/photo"    
46                 android:drawableTop="@drawable/icon_3"    
47                 style="@style/main_tab_bottom" />  
48             <RadioButton    
49                 android:id="@+id/radio_button3"    
50                 android:tag="radio_button3"    
51                 android:layout_marginTop="2.0dip"    
52                 android:text="@string/music"    
53                 android:drawableTop="@drawable/icon_4"    
54                 style="@style/main_tab_bottom" />  
55             <RadioButton    
56                 android:id="@+id/radio_button4"    
57                 android:tag="radio_button4"    
58                 android:layout_marginTop="2.0dip"    
59                 android:text="@string/setting"    
60                 android:drawableTop="@drawable/icon_5"    
61                 style="@style/main_tab_bottom" />  
62         </RadioGroup>  
63     </LinearLayout>  
64 </TabHost>  

隐藏了系统默认的Widget,取而代之的是带有图片的Button

java

代码语言:javascript
代码运行次数:0
运行
AI代码解释
复制
 1 package com.iteye.androidtoast;   
 2   
 3 import android.app.TabActivity;   
 4 import android.content.Intent;   
 5 import android.os.Bundle;   
 6 import android.widget.RadioGroup;   
 7 import android.widget.RadioGroup.OnCheckedChangeListener;   
 8 import android.widget.TabHost;   
 9   
10 public class MainActivity extends TabActivity implements OnCheckedChangeListener{   
11     /** Called when the activity is first created. */  
12     private TabHost mHost;   
13     private RadioGroup radioderGroup;   
14     @Override  
15     public void onCreate(Bundle savedInstanceState) {   
16         super.onCreate(savedInstanceState);   
17         setContentView(R.layout.maintabs);   
18         //实例化TabHost   
19         mHost=this.getTabHost();   
20            
21         //添加选项卡   
22         mHost.addTab(mHost.newTabSpec("ONE").setIndicator("ONE")   
23                     .setContent(new Intent(this,OneActivity.class)));   
24         mHost.addTab(mHost.newTabSpec("TWO").setIndicator("TWO")   
25                 .setContent(new Intent(this,TwoActivity.class)));   
26         mHost.addTab(mHost.newTabSpec("THREE").setIndicator("THREE")   
27                 .setContent(new Intent(this,ThreeActivity.class)));   
28         mHost.addTab(mHost.newTabSpec("FOUR").setIndicator("FOUR")   
29                 .setContent(new Intent(this,FourActivity.class)));   
30         mHost.addTab(mHost.newTabSpec("FIVE").setIndicator("FIVE")   
31                 .setContent(new Intent(this,FiveActivity.class)));   
32            
33         radioderGroup = (RadioGroup) findViewById(R.id.main_radio);   
34         radioderGroup.setOnCheckedChangeListener(this);   
35     }   
36     @Override  
37     public void onCheckedChanged(RadioGroup group, int checkedId) {   
38         switch(checkedId){   
39         case R.id.radio_button0:   
40             mHost.setCurrentTabByTag("ONE");   
41             break;   
42         case R.id.radio_button1:   
43             mHost.setCurrentTabByTag("TWO");   
44             break;   
45         case R.id.radio_button2:   
46             mHost.setCurrentTabByTag("THREE");   
47             break;   
48         case R.id.radio_button3:   
49             mHost.setCurrentTabByTag("FOUR");   
50             break;   
51         case R.id.radio_button4:   
52             mHost.setCurrentTabByTag("FIVE");   
53             break;   
54         }          
55     }   
56 }  
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2013-02-28 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
暂无评论
推荐阅读
编辑精选文章
换一批
新浪微博布局学习——妙用TabHost
为了更好的开发Android应用程序,除了熟练掌握基本的UI组件和API外,还需要掌握一些技巧,而这些技巧可以通过阅读一些代码来提高,本系列将与大家分享一些新浪微博布局方面的收获,欢迎交流!
飞雪无情
2018/08/28
3730
新浪微博布局学习——妙用TabHost
关于安卓开发实现底部菜单栏(已过时做法,不建议使用)
将TabHost的标签放在底部 直接上代码 主代码: 1 package sdut; 2 3 import com.example.sdutfriends.R; 4 5 import android.app.AlertDialog; 6 import android.app.TabActivity; 7 import android.content.DialogInterface; 8 import android.content.Intent; 9 import android.os
听着music睡
2018/05/18
9610
选项卡(TabHost)使用
使用方式: 从TabActivity中用getTabHost()方法获取TabHost,然后设置标签内容
李小白是一只喵
2020/04/24
1.8K0
Android应用底部导航栏(选项卡)实例
现在很多android的应用都采用底部导航栏的功能,这样可以使得用户在使用过程中随意切换不同的页面,现在我采用TabHost组件来自定义一个底部的导航栏的功能。 我们先看下该demo实例的框架图: 其
欢醉
2018/01/22
1.5K0
Android应用底部导航栏(选项卡)实例
【Android 应用开发】Android - TabHost 选项卡功能用法详解
源码下载地址 : http://download.csdn.net/detail/han1202012/6845105
韩曙亮
2023/03/27
1.2K0
【Android 应用开发】Android - TabHost 选项卡功能用法详解
过时但仍值得学习的选项卡TabHost
今天来了解一个过时的组件,了解的目的不是学会用起来开发,而是了解这种界面的设计和其特点,后期可以用其他方式来替代。 一、TabHost概述 TabHost是一种非常实用的组件,TabHost可以很方便地在窗口上放置多个标签页,每个标签页相当于获得了一个与外部容器相同大小的组件摆放区域。通过这种方式,就可以在一个容器里放置更多组件。 TabHost是整个Tab的容器,包含TabWidget和FrameLayout两个部分,TabWidget是每个Tab的标签,FrameLayout是
分享达人秀
2018/02/05
1.6K0
过时但仍值得学习的选项卡TabHost
Android开发(9) 选项卡的切换
我们想实现的效果是点击切换的选项卡卡部分,主显示区的内容随之改变。那么我们看下页面布局代码
张云飞Vir
2020/03/13
1.6K0
Android UI控件系列:TabWidget(切换卡)
Android UI控件系列:TabWidget(切换卡) Tab选项卡类似与电话本的界面,通过多个标签切换不同的内容,要实现这个效果,首先要知道TabHost,它是一个用来存放多个Tab标签的容器,每一个Tab都可以对应自己的布局,比如,电话本中的Tab布局就是一个线性布局 要使用TabHost,首先要通过getTabHost方法获取TabHost的对象,然后通过addTab方法来向TabHost中添加Tab,当然每个Tab在切换时都会产生一个事件,要捕捉这个事件,需要设置TabActivity的事件监听
用户1289394
2018/02/26
1.7K0
Android UI控件系列:TabWidget(切换卡)
【Android从零单排系列二十八】《Android视图控件——TabHost》
小伙伴们,在上文中我们介绍了Android视图组件HorizontalScrollView,本文我们继续盘点,介绍一下视图控件的TableHost。
再见孙悟空_
2023/07/17
3430
Android学习Tabhost、gallery、listview、imageswitcher
Tabhost控件又称分页控件,在很多的开发语言中都存在。它可以拥有多个标签页,每个标签页可以拥有不同的内容。android中,一个标签页可以放 一个view或者一个activity。TabHost是标签控件类的核心类,也是标签的集合。 1.tabhost定义 android控件中有封装好的tab控件,直接拖一个到xml文件中。下面的代码都是tab控件自己生成的。 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/and
水击三千
2018/02/27
1.2K0
Android学习Tabhost、gallery、listview、imageswitcher
1.TabActivity、视图树、动画
整个页面为TabActivity, 其中对TabWidget进行了一些改变,当切换页签时页签后面红色背景会以Translate动画形式移动到相对应的页签后。 布局 TabHost、TabWidget、FrameLayout的id必须是系统定义的, 因为可以直接get获取控件,上面的Tab标签一般不写原生的,自己写。 把原生的TabWidget隐藏,用了个垂直的LinearLayout写, 下面是FrameLayout,也是TabHost必须写的 <RelativeLayout xmlns:android=
六月的雨
2018/05/14
7900
速读原著-Android应用开发入门教程(使用Tab组织UI)
Tab 用于在一个屏幕中将不同的子屏幕组织到一起,用不同的 Tab 区分。 参考示例程序:Content By Intent(ApiDemo=>Views=>Tabs=>Content By Intent) 源代码:com/example/android/apis/view/Tab3.java Tab3 程序的运行结果如图所示:
cwl_java
2020/02/13
4830
16.FragmentTabHost用法
FragmentTabHost组成 Tabhost,TabWidget,切换的内容容器FrameLayout 层级关系 ----FragmentTabHost |-----TabWidget |-----FrameLayout 布局实现 实现tabhost采用android.support.v4.app.FragmentTabHost 注意 id:@android:id/tabhost 实现tabWidget 注意 id:@android:id/tabs 实现FrameLa
六月的雨
2018/05/14
8830
FragmentTabHost用法
FragmentTabHost组成 Tabhost,TabWidget,切换的内容容器FrameLayout 布局实现 实现tabhost采用android.support.v4.app.FragmentTabHost 注意 id:@android:id/tabhost 实现tabWidget 注意 id:@android:id/tabs 实现FrameLayout 注意 1. id: @android:id/tabcontent 2. 此容器已经被废除,但在布局中必须有 实现自定义的内
六月的雨
2022/01/12
3830
FragmentTabHost用法
Android开发之ViewPager+Fragment+FragmentTabHost实现底部菜单
在Android开发中,底部菜单是经常要使用的,如微信、微博、支付宝等,实现底部菜单有好几种方式,大致分为: 通过TabWidget实现 隐藏TabWidget,通过RadioGroup和RadioButton实现底部菜单栏 通过FragmentTabHost实现 通过5.0以后的TabLayout实现 通过最近推出的 Bottom navigation ---- 本文的主题是 ViewPager+Fragment+FragmentTabHost 实现底部菜单 1、构造4个Fragment,每个布局类似如下
YungFan
2018/04/24
1.3K0
Android Fragment 简单实例
  Android上的界面展示都是通过Activity实现的。Activity实在是太经常使用了。我相信大家都已经很熟悉了,这里就不再赘述。 可是Activity也有它的局限性,相同的界面在手机上显示可能很好看,在平板上就未必了,由于平板的屏幕很大。手机的界面放在平板上可能会有过分被拉长、控件间距过大等情况。这个时候更好的体验效果是在Activity中嵌入”小Activity”。然后每个”小Activity”又能够拥有自己的布局。这就是Fragment碎片技术。
全栈程序员站长
2022/07/10
6250
Android Fragment 简单实例
首页-底部Tab导航(菜单栏)的实现:FragmentTabHost+ViewPager+Fragment
Android开发中使用底部菜单栏的频次非常高,主要的实现手段有以下: - TabWidget - 隐藏TabWidget,使用RadioGroup和RadioButton - FragmentTabHost - 5.0以后的TabLayout - 最近推出的 Bottom navigation
Carson.Ho
2019/02/22
1.9K0
仿qq底部Tab导航
版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/gdutxiaoxu/article/details/52826810
程序员徐公
2018/09/18
2K0
仿qq底部Tab导航
Carson带你学Android:手把手教你优雅实现首页-底部Tab导航(菜单栏)
今天带大家来探索下如何用Fragment+FragmentTabHost++ViewPager实现底部菜单栏
Carson.Ho
2022/03/24
9190
Carson带你学Android:手把手教你优雅实现首页-底部Tab导航(菜单栏)
多功能时钟应用
近几天做了个多功能时钟,分享一下 主界面 对应的代码: MainActivity.java package com.example.clock; import android.a
提莫队长
2019/02/21
1K0
相关推荐
新浪微博布局学习——妙用TabHost
更多 >
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档
查看详情【社区公告】 技术创作特训营有奖征文
本文部分代码块支持一键运行,欢迎体验
本文部分代码块支持一键运行,欢迎体验