深入浅出Tabhost+简单入门Demo
小伙伴们在手机上逛淘宝的时候,会发现在淘宝的下面有个按钮,分别是首页、微淘、社区、购物车和我的淘宝,点击不同的按钮会跳转到不同的页面,目前小编所接手的这个项目,也需要用到类似这样的功能,小编就发挥网络的强大力量,原来人家使用的技术叫做Tabhost,用Tabhost来控制各个选项卡的切换,Tabhost的实现分为两种,一种是继承自TabActivity,继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。只要定义具体Tab内容布局就行了。另一种是不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是 @android:id/tabs,FrameLayout的id必须是@android:id/tabcontent,TabHost的id可以自定义。今天这篇博文,小编就来简单的介绍一下有关于Tabhost的故事,还请小伙伴多多指教哦`(*∩_∩*)′!
首先,我们来看第一种实现方式,继承自TabActivity,首先我们来看xml的布局代码,具体代码如下所示:
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <!-- 第一个布局 --> <LinearLayout android:id="@+id/view_one" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView_one" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="等一个故事" /> </LinearLayout> <!-- 第二个布局 --> <LinearLayout android:id="@+id/view_two" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView_two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="当幸福来敲门" /> </LinearLayout> <!-- 第三个布局 --> <LinearLayout android:id="@+id/view_three" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView_three" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="微笑闪亮未来" /> </LinearLayout> </FrameLayout>
接着,我们来看java类中的代码,具体代码如下所示:
package com.example.tabhost_trynew; import com.example.tabhost_trynew.R; import android.app.TabActivity; import android.os.Bundle; import android.view.LayoutInflater; import android.view.Menu; import android.widget.TabHost; import android.widget.TabHost.OnTabChangeListener; public class MainActivity extends TabActivity{ @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setTitle("TabDemoActivity"); TabHost tabHost = getTabHost(); LayoutInflater.from(this).inflate(R.layout.activity_main, tabHost.getTabContentView(), true); tabHost.addTab(tabHost.newTabSpec("tab_one").setIndicator("tab_one", getResources().getDrawable(R.drawable.flower)) .setContent(R.id.view_one)); tabHost.addTab(tabHost.newTabSpec("tab_two").setIndicator("tab_two", getResources().getDrawable(R.drawable.flower)) .setContent(R.id.view_two)); tabHost.addTab(tabHost.newTabSpec("tab_three").setIndicator("tab_three", getResources().getDrawable(R.drawable.flower)) .setContent(R.id.view_three)); //标签切换事件处理,setOnTabChangedListener tabHost.setOnTabChangedListener(new OnTabChangeListener(){ @Override public void onTabChanged(String tabId) { if (tabId.equals("tab_one")) { //第一个标签 } if (tabId.equals("tab_two")) { //第二个标签 } if (tabId.equals("tab_three")) { //第三个标签 } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } }
我们来看一下运行效果,如下图所示:
接着,我们来看第二种实现方式,不继承自TabActivity,首先,我们来看xml的布局代码,具体代码如下所示:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity" > <TabHost android:id="@+id/tabhost" android:layout_width="match_parent" android:layout_height="wrap_content"> <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" > </TabWidget> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" > <!-- 第一个tab的布局 --> <LinearLayout android:id="@+id/tab1" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="等一个故事" /> </LinearLayout> <!-- 第二个tab的布局 --> <LinearLayout android:id="@+id/tab2" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="当幸福来敲门" /> </LinearLayout> <!-- 第三个tab的布局 --> <LinearLayout android:id="@+id/tab3" android:layout_width="match_parent" android:layout_height="match_parent" > <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="微笑闪亮未来" /> </LinearLayout> </FrameLayout> </LinearLayout> </TabHost> </LinearLayout>
接着,我们来看java类中的代码,代码如下所示:
package com.example.tabhost_try2; import android.os.Bundle; import android.app.Activity; import android.view.Menu; import android.widget.TabHost; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //初始化TabHost容器 TabHost th=(TabHost)findViewById(R.id.tabhost); th.setup(); //在TabHost创建标签,然后设置:标题/图标/标签页布局 th.addTab(th.newTabSpec("tab1").setIndicator("tab_one",getResources().getDrawable(R.drawable.flower)).setContent(R.id.tab1)); th.addTab(th.newTabSpec("tab2").setIndicator("tab_two",getResources().getDrawable(R.drawable.flower)).setContent(R.id.tab2)); th.addTab(th.newTabSpec("tab3").setIndicator("tab_three",getResources().getDrawable(R.drawable.flower)).setContent(R.id.tab3)); //上面的null可以为getResources().getDrawable(R.drawable.图片名)设置图标 } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }
我们来看一下运行效果,如下所示:
小编寄语:该博客小编主要简单的介绍了Tabhost的相关知识,一个是不继承TabActivity,一个是继承自TabActivity;还是那句话对于小编来说,既是挑战更是机遇,因为知识都是相通的,再者来说,在小编的程序人生中,留下最珍贵的记忆,虽然以后小编不一定从事安卓这个行业,代码世界里,很多种事,有的甜蜜,有的温馨,有的婉转成歌,有的绵延不息,在这些故事里,我们唯一的共通之处就是,某年,某月,某个波澜不惊的日子里,曾经很爱很爱你!爱你--这段接触Android日子里,安卓带给小编的种种的惊喜。
深入浅出Tabhost+简单入门Demo的更多相关文章
- Quartz学习——Quartz简单入门Demo(二)
要学习Quartz框架,首先大概了解了Quartz的基本知识后,在通过简单的例子入门,一步一个脚印的走下去. 下面介绍Quartz入门的示例,由于Quartz的存储方式分为RAM和JDBC,分别对这两 ...
- gflag的简单入门demo
gflags 一. 下载与安装 这里直接使用包管理器安装: sudo apt install libgflags-dev 二. gflags的简单使用 1. 定义需要的类型 格式: DEFINE_类型 ...
- ECharts简单入门demo
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...
- Dubbo入门—搭建一个最简单的Demo框架
一.Dubbo背景和简介 1.电商系统的演进 Dubbo开始于电商系统,因此在这里先从电商系统的演变讲起. a.单一应用框架(ORM) 当网站流量很小时,只需一个应用,将所有功能如下单支付等都部署在一 ...
- Dubbo入门---搭建一个最简单的Demo框架(转)
Dubbo背景和简介 Dubbo开始于电商系统,因此在这里先从电商系统的演变讲起. 单一应用框架(ORM) 当网站流量很小时,只需一个应用,将所有功能如下单支付等都部署在一起,以减少部署节点和成本. ...
- C#中缓存的使用 ajax请求基于restFul的WebApi(post、get、delete、put) 让 .NET 更方便的导入导出 Excel .net core api +swagger(一个简单的入门demo 使用codefirst+mysql) C# 位运算详解 c# 交错数组 c# 数组协变 C# 添加Excel表单控件(Form Controls) C#串口通信程序
C#中缓存的使用 缓存的概念及优缺点在这里就不多做介绍,主要介绍一下使用的方法. 1.在ASP.NET中页面缓存的使用方法简单,只需要在aspx页的顶部加上一句声明即可: <%@ Outp ...
- Dubbo入门介绍---搭建一个最简单的Demo框架
Dubbo入门---搭建一个最简单的Demo框架 置顶 2017年04月17日 19:10:44 是Guava不是瓜娃 阅读数:320947 标签: dubbozookeeper 更多 个人分类: D ...
- Maven+SpringMVC+Dubbo 简单的入门demo配置
转载自:https://cloud.tencent.com/developer/article/1010636 之前一直听说dubbo,是一个很厉害的分布式服务框架,而且巴巴将其开源,这对于咱们广大程 ...
- Okio 1.9简单入门
Okio 1.9简单入门 Okio库是由square公司开发的,补充了java.io和java.nio的不足,更加方便,快速的访问.存储和处理你的数据.而OkHttp的底层也使用该库作为支持. 该库极 ...
随机推荐
- win10下python环境变量设置
我用的是python_2.7.3.msi,从官网下载之后,一路按照默认进行安装. 安装之后配置环境变量的步骤如下: 1,点“我的电脑”,右键选“属性” 2,选择“高级系统设置”--->选“环境变 ...
- C语言程序设计第三次作业——选择结构(一)
(一)改错题 错误信息: 错误原因:y=1/x后没加分号 改正方法:在其后加上分号 错误信息: 错误原因:if语句后接了:,使else语句找不到对应的if 改正方法:删掉if后的分号 错误信息: 错误 ...
- 一个成功的 Git 分支模型(适用于商业应用开发)
在这篇文章中,我将推广一下大约一年前我介绍过的一些项目(公私皆有)中使用的开发模型,它们的结果都非常成功.有段时间我非常想写出来分享一下,但是我至今才抽出时间来.我不会言及任何项目细节,仅讨论分支策略 ...
- Mybatis自动生成实体类和实体映射工具
Mybatis Mysql生成实体类 用到的Lib包: mybatis-generator-core-1.3.2.jarmysql-connector-java-5.1.30.jar 1. 创建一个文 ...
- Async分析
1:android在新版本中不允许UI线程访问网络,但是如果需要访问网络又改怎么办呐?这里有很多解决方案,比如新开一个线程,在新线程中进行访问,然后访问数据,返回后可能会更新界面也可能不更新界面,这 ...
- Java中的网络支持InetAddress&URL
针对网络通信的不同层次,Java提供的网络功能有四大类 InetAddress:用于标识网络上的硬件资源.(说白了就是IP地址的相关信息) URL:统一资源定位符,通过URL可以直接读取或写入网络上的 ...
- 解析配置文件redis.conf
units单位: # 1k => 1000 bytes # 1kb => 1024 bytes # 1m => 1000000 bytes # 1mb => 1024*1024 ...
- 解决Error: ENOENT: no such file or directory, scandir 'D:\IdeaWork\code-front-jet\node_modules\.npminstall\node-sass\3.7.0\node-sass\vendor'
在使用npm安装node-sass的时候,可能会出现如下的报错: Error: ENOENT: no such file or directory, scandir 'D:\IdeaWork\code ...
- Helm 架构 - 每天5分钟玩转 Docker 容器技术(161)
在实践之前,我们先来看看 Helm 的架构. Helm 有两个重要的概念:chart 和 release. chart 是创建一个应用的信息集合,包括各种 Kubernetes 对象的配置模板.参数定 ...
- Node.js 系统
稳定性: 4 - API 冻结 提供一些基本的操作系统相关函数. 使用 require('os') 访问这个模块. os.tmpdir() 返回操作系统的默认临时文件夹 os.endianness() ...