TabHost三种方法

2024-08-31 23:58
文章标签 方法 三种 tabhost

本文主要是介绍TabHost三种方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!


方式一:TabHost继承TabActivity方法相当简单

首先看布局文件activity_main.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" ><!-- 第一个布局 --><LinearLayoutandroid:id="@+id/view1"android:layout_width="match_parent"android:layout_height="match_parent" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Tab1" /></LinearLayout><!-- 第二个布局 --><LinearLayoutandroid:id="@+id/view2"android:layout_width="match_parent"android:layout_height="match_parent" ><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Tab2" /></LinearLayout><!-- 第三个布局 --><LinearLayoutandroid:id="@+id/view3"android:layout_width="match_parent"android:layout_height="match_parent" ><TextViewandroid:id="@+id/textView3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Tab3" /></LinearLayout></FrameLayout>

然后就是一个activity就行了MainActivity

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.FrameLayout;
import android.widget.TabHost;
import android.widget.TabHost.OnTabChangeListener; 
import android.widget.TabHost.TabSpec;public class MainActivity extends TabActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);//TabHost extends FrameLayoutTabHost tabHost = getTabHost();把我们的布局文件添加到tabHost的FrameLayout下面 LayoutInflater.from(this).inflate(R.layout.activity_main,tabHost.getTabContentView(), true);/*** public void addTab(TabSpec tabSpec)* tabSpec:A tab has a tab indicator, content, and a tag * */tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("tab1", getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.view1));tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab2").setContent(R.id.view2));tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("tab3").setContent(R.id.view3));//setOnTabChangedListenertabHost.setOnTabChangedListener(new OnTabChangeListener(){  @Overridepublic void onTabChanged(String tabId) {if (tabId.equals("tab1")) {   }if (tabId.equals("tab2")) {   }if (tabId.equals("tab3")) {  }}            }); }
}

++++++++++++++++++++++++++++++++++++++++++方式二+++++++++++++++++++++++++++++++++++

方式二:TabHost继承Activity(xml布局实现)

activity_main.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是盛放Tab的容器。TabHost必须包含一个 TabWidget和一个FrameLayout --><TabHostandroid:id="@+id/tabhost"android:layout_width="match_parent"android:layout_height="match_parent"><!-- 这里添加一个布局,原因是 --><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><!-- TabWidget用来显示选项卡 --><TabWidgetandroid:id="@android:id/tabs"android:layout_width="match_parent"android:layout_height="wrap_content" ></TabWidget><!-- 用来显示选项卡对应的内容 --><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="match_parent" ><!-- 第一个tab的布局 --><LinearLayoutandroid:id="@+id/tab1"android:layout_width="match_parent"android:layout_height="match_parent" ><TextViewandroid:id="@+id/textView1"android:layout_width="match_parent"android:layout_height="match_parent"android:text="林炳东" /></LinearLayout><!-- 第二个tab的布局 --><LinearLayoutandroid:id="@+id/tab2"android:layout_width="match_parent"android:layout_height="match_parent" ><TextViewandroid:id="@+id/textView2"android:layout_width="match_parent"android:layout_height="match_parent"android:text="张小媛" /></LinearLayout><!-- 第三个tab的布局 --><LinearLayoutandroid:id="@+id/tab3"android:layout_width="match_parent"android:layout_height="match_parent" ><TextViewandroid:id="@+id/textView3"android:layout_width="match_parent"android:layout_height="match_parent"android:text="马贝贝" /></LinearLayout></FrameLayout></LinearLayout></TabHost> </LinearLayout>
MainActivity

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 {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TabHost th=(TabHost)findViewById(R.id.tabhost);// 作用是来初始化我们的TabHost容器:th.setup();            th.addTab(th.newTabSpec("tab1").setIndicator("tab1",getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab1));th.addTab(th.newTabSpec("tab2").setIndicator("tab2",null).setContent(R.id.tab2));th.addTab(th.newTabSpec("tab3").setIndicator("tab3",null).setContent(R.id.tab3));}
}

+++++++++++++++++++++++++++++++++++++方式三+++++++++++++++++++++++++++++++++++++++++

拆分方法(跟方法二类似)

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TabHostandroid:id="@+id/tabhost"         android:layout_width="match_parent"android:layout_height="match_parent" ><LinearLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TabWidgetandroid:id="@android:id/tabs"android:layout_width="match_parent"android:layout_height="wrap_content" ></TabWidget><FrameLayoutandroid:id="@android:id/tabcontent"android:layout_width="match_parent"android:layout_height="match_parent" ></FrameLayout></LinearLayout></TabHost></LinearLayout>

tab1.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/LinearLayout01" android:layout_width="wrap_content"android:layout_height="wrap_content"><TextView android:text="我是标签1的内容喔"android:id="@+id/TextView01" android:layout_width="wrap_content"android:layout_height="wrap_content"></TextView></LinearLayout>

tab2.xml

<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android"android:id="@+id/LinearLayout02"android:layout_width="wrap_content"android:layout_height="wrap_content"><TextView android:text="标签2"android:id="@+id/TextView01" android:layout_width="wrap_content"android:layout_height="wrap_content" />
</LinearLayout>

MainActivity

package com.example.tabhost_try3;import android.os.Bundle;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.widget.TabHost;public class MainActivity extends Activity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);TabHost m = (TabHost) findViewById(R.id.tabhost);m.setup();LayoutInflater i = LayoutInflater.from(this);i.inflate(R.layout.tab1, m.getTabContentView());i.inflate(R.layout.tab2, m.getTabContentView());m.addTab(m.newTabSpec("tab1").setIndicator("tab1").setContent(R.id.LinearLayout01));m.addTab(m.newTabSpec("tab2").setIndicator("tab2").setContent(R.id.LinearLayout02));}
}


这篇关于TabHost三种方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1125367

相关文章

Python常用命令提示符使用方法详解

《Python常用命令提示符使用方法详解》在学习python的过程中,我们需要用到命令提示符(CMD)进行环境的配置,:本文主要介绍Python常用命令提示符使用方法的相关资料,文中通过代码介绍的... 目录一、python环境基础命令【Windows】1、检查Python是否安装2、 查看Python的安

Maven 配置中的 <mirror>绕过 HTTP 阻断机制的方法

《Maven配置中的<mirror>绕过HTTP阻断机制的方法》:本文主要介绍Maven配置中的<mirror>绕过HTTP阻断机制的方法,本文给大家分享问题原因及解决方案,感兴趣的朋友一... 目录一、问题场景:升级 Maven 后构建失败二、解决方案:通过 <mirror> 配置覆盖默认行为1. 配置示

SpringBoot排查和解决JSON解析错误(400 Bad Request)的方法

《SpringBoot排查和解决JSON解析错误(400BadRequest)的方法》在开发SpringBootRESTfulAPI时,客户端与服务端的数据交互通常使用JSON格式,然而,JSON... 目录问题背景1. 问题描述2. 错误分析解决方案1. 手动重新输入jsON2. 使用工具清理JSON3.

使用jenv工具管理多个JDK版本的方法步骤

《使用jenv工具管理多个JDK版本的方法步骤》jenv是一个开源的Java环境管理工具,旨在帮助开发者在同一台机器上轻松管理和切换多个Java版本,:本文主要介绍使用jenv工具管理多个JD... 目录一、jenv到底是干啥的?二、jenv的核心功能(一)管理多个Java版本(二)支持插件扩展(三)环境隔

Java中Map.Entry()含义及方法使用代码

《Java中Map.Entry()含义及方法使用代码》:本文主要介绍Java中Map.Entry()含义及方法使用的相关资料,Map.Entry是Java中Map的静态内部接口,用于表示键值对,其... 目录前言 Map.Entry作用核心方法常见使用场景1. 遍历 Map 的所有键值对2. 直接修改 Ma

Mybatis Plus Join使用方法示例详解

《MybatisPlusJoin使用方法示例详解》:本文主要介绍MybatisPlusJoin使用方法示例详解,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,... 目录1、pom文件2、yaml配置文件3、分页插件4、示例代码:5、测试代码6、和PageHelper结合6

Java中实现线程的创建和启动的方法

《Java中实现线程的创建和启动的方法》在Java中,实现线程的创建和启动是两个不同但紧密相关的概念,理解为什么要启动线程(调用start()方法)而非直接调用run()方法,是掌握多线程编程的关键,... 目录1. 线程的生命周期2. start() vs run() 的本质区别3. 为什么必须通过 st

C#之List集合去重复对象的实现方法

《C#之List集合去重复对象的实现方法》:本文主要介绍C#之List集合去重复对象的实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录C# List集合去重复对象方法1、测试数据2、测试数据3、知识点补充总结C# List集合去重复对象方法1、测试数据

SpringBoot读取ZooKeeper(ZK)属性的方法实现

《SpringBoot读取ZooKeeper(ZK)属性的方法实现》本文主要介绍了SpringBoot读取ZooKeeper(ZK)属性的方法实现,强调使用@ConfigurationProperti... 目录1. 在配置文件中定义 ZK 属性application.propertiesapplicati

MyBatis设计SQL返回布尔值(Boolean)的常见方法

《MyBatis设计SQL返回布尔值(Boolean)的常见方法》这篇文章主要为大家详细介绍了MyBatis设计SQL返回布尔值(Boolean)的几种常见方法,文中的示例代码讲解详细,感兴趣的小伙伴... 目录方案一:使用COUNT查询存在性(推荐)方案二:条件表达式直接返回布尔方案三:存在性检查(EXI