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

相关文章

golang中reflect包的常用方法

《golang中reflect包的常用方法》Go反射reflect包提供类型和值方法,用于获取类型信息、访问字段、调用方法等,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录reflect包方法总结类型 (Type) 方法值 (Value) 方法reflect包方法总结

C# 比较两个list 之间元素差异的常用方法

《C#比较两个list之间元素差异的常用方法》:本文主要介绍C#比较两个list之间元素差异,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1. 使用Except方法2. 使用Except的逆操作3. 使用LINQ的Join,GroupJoin

MySQL查询JSON数组字段包含特定字符串的方法

《MySQL查询JSON数组字段包含特定字符串的方法》在MySQL数据库中,当某个字段存储的是JSON数组,需要查询数组中包含特定字符串的记录时传统的LIKE语句无法直接使用,下面小编就为大家介绍两种... 目录问题背景解决方案对比1. 精确匹配方案(推荐)2. 模糊匹配方案参数化查询示例使用场景建议性能优

关于集合与数组转换实现方法

《关于集合与数组转换实现方法》:本文主要介绍关于集合与数组转换实现方法,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、Arrays.asList()1.1、方法作用1.2、内部实现1.3、修改元素的影响1.4、注意事项2、list.toArray()2.1、方

Python中注释使用方法举例详解

《Python中注释使用方法举例详解》在Python编程语言中注释是必不可少的一部分,它有助于提高代码的可读性和维护性,:本文主要介绍Python中注释使用方法的相关资料,需要的朋友可以参考下... 目录一、前言二、什么是注释?示例:三、单行注释语法:以 China编程# 开头,后面的内容为注释内容示例:示例:四

一文详解Git中分支本地和远程删除的方法

《一文详解Git中分支本地和远程删除的方法》在使用Git进行版本控制的过程中,我们会创建多个分支来进行不同功能的开发,这就容易涉及到如何正确地删除本地分支和远程分支,下面我们就来看看相关的实现方法吧... 目录技术背景实现步骤删除本地分支删除远程www.chinasem.cn分支同步删除信息到其他机器示例步骤

在Golang中实现定时任务的几种高效方法

《在Golang中实现定时任务的几种高效方法》本文将详细介绍在Golang中实现定时任务的几种高效方法,包括time包中的Ticker和Timer、第三方库cron的使用,以及基于channel和go... 目录背景介绍目的和范围预期读者文档结构概述术语表核心概念与联系故事引入核心概念解释核心概念之间的关系

在Linux终端中统计非二进制文件行数的实现方法

《在Linux终端中统计非二进制文件行数的实现方法》在Linux系统中,有时需要统计非二进制文件(如CSV、TXT文件)的行数,而不希望手动打开文件进行查看,例如,在处理大型日志文件、数据文件时,了解... 目录在linux终端中统计非二进制文件的行数技术背景实现步骤1. 使用wc命令2. 使用grep命令

Python中Tensorflow无法调用GPU问题的解决方法

《Python中Tensorflow无法调用GPU问题的解决方法》文章详解如何解决TensorFlow在Windows无法识别GPU的问题,需降级至2.10版本,安装匹配CUDA11.2和cuDNN... 当用以下代码查看GPU数量时,gpuspython返回的是一个空列表,说明tensorflow没有找到

XML重复查询一条Sql语句的解决方法

《XML重复查询一条Sql语句的解决方法》文章分析了XML重复查询与日志失效问题,指出因DTO缺少@Data注解导致日志无法格式化、空指针风险及参数穿透,进而引发性能灾难,解决方案为在Controll... 目录一、核心问题:从SQL重复执行到日志失效二、根因剖析:DTO断裂引发的级联故障三、解决方案:修复