深入浅出Tabhost+简单入门Demo

简介:        小伙伴们在手机上逛淘宝的时候,会发现在淘宝的下面有个按钮,分别是首页、微淘、社区、购物车和我的淘宝,点击不同的按钮会跳转到不同的页面,目前小编所接手的这个项目,也需要用到类似这样的功能,小编就发挥网络的强大力量,原来人家使用的技术叫做Tabhost,用Tabhost来控制各个选项卡的切换,Tabhost的实现分为两种,一种是继承自TabActivity,继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。

       小伙伴们在手机上逛淘宝的时候,会发现在淘宝的下面有个按钮,分别是首页、微淘、社区、购物车和我的淘宝,点击不同的按钮会跳转到不同的页面,目前小编所接手的这个项目,也需要用到类似这样的功能,小编就发挥网络的强大力量,原来人家使用的技术叫做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日子里,安卓带给小编的种种的惊喜。    

目录
相关文章
|
1天前
|
云安全 人工智能 自然语言处理
|
6天前
|
搜索推荐 编译器 Linux
一个可用于企业开发及通用跨平台的Makefile文件
一款适用于企业级开发的通用跨平台Makefile,支持C/C++混合编译、多目标输出(可执行文件、静态/动态库)、Release/Debug版本管理。配置简洁,仅需修改带`MF_CONFIGURE_`前缀的变量,支持脚本化配置与子Makefile管理,具备完善日志、错误提示和跨平台兼容性,附详细文档与示例,便于学习与集成。
314 116
|
8天前
|
数据采集 人工智能 自然语言处理
Meta SAM3开源:让图像分割,听懂你的话
Meta发布并开源SAM 3,首个支持文本或视觉提示的统一图像视频分割模型,可精准分割“红色条纹伞”等开放词汇概念,覆盖400万独特概念,性能达人类水平75%–80%,推动视觉分割新突破。
606 53
Meta SAM3开源:让图像分割,听懂你的话
|
21天前
|
域名解析 人工智能
【实操攻略】手把手教学,免费领取.CN域名
即日起至2025年12月31日,购买万小智AI建站或云·企业官网,每单可免费领1个.CN域名首年!跟我了解领取攻略吧~
|
5天前
|
人工智能 Java API
Java 正式进入 Agentic AI 时代:Spring AI Alibaba 1.1 发布背后的技术演进
Spring AI Alibaba 1.1 正式发布,提供极简方式构建企业级AI智能体。基于ReactAgent核心,支持多智能体协作、上下文工程与生产级管控,助力开发者快速打造可靠、可扩展的智能应用。
|
4天前
|
弹性计算 人工智能 Cloud Native
阿里云无门槛和有门槛优惠券解析:学生券,满减券,补贴券等优惠券领取与使用介绍
为了回馈用户与助力更多用户节省上云成本,阿里云会经常推出各种优惠券相关的活动,包括无门槛优惠券和有门槛优惠券。本文将详细介绍阿里云无门槛优惠券的领取与使用方式,同时也会概述几种常见的有门槛优惠券,帮助用户更好地利用这些优惠,降低云服务的成本。
269 132
|
8天前
|
机器学习/深度学习 人工智能 自然语言处理
AgentEvolver:让智能体系统学会「自我进化」
AgentEvolver 是一个自进化智能体系统,通过自我任务生成、经验导航与反思归因三大机制,推动AI从“被动执行”迈向“主动学习”。它显著提升强化学习效率,在更少参数下实现更强性能,助力智能体持续自我迭代。开源地址:https://github.com/modelscope/AgentEvolver
419 29
|
15天前
|
安全 Java Android开发
深度解析 Android 崩溃捕获原理及从崩溃到归因的闭环实践
崩溃堆栈全是 a.b.c?Native 错误查不到行号?本文详解 Android 崩溃采集全链路原理,教你如何把“天书”变“说明书”。RUM SDK 已支持一键接入。
727 223