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

相关文章

Java 中的 @SneakyThrows 注解使用方法(简化异常处理的利与弊)

《Java中的@SneakyThrows注解使用方法(简化异常处理的利与弊)》为了简化异常处理,Lombok提供了一个强大的注解@SneakyThrows,本文将详细介绍@SneakyThro... 目录1. @SneakyThrows 简介 1.1 什么是 Lombok?2. @SneakyThrows

判断PyTorch是GPU版还是CPU版的方法小结

《判断PyTorch是GPU版还是CPU版的方法小结》PyTorch作为当前最流行的深度学习框架之一,支持在CPU和GPU(NVIDIACUDA)上运行,所以对于深度学习开发者来说,正确识别PyTor... 目录前言为什么需要区分GPU和CPU版本?性能差异硬件要求如何检查PyTorch版本?方法1:使用命

Qt实现网络数据解析的方法总结

《Qt实现网络数据解析的方法总结》在Qt中解析网络数据通常涉及接收原始字节流,并将其转换为有意义的应用层数据,这篇文章为大家介绍了详细步骤和示例,感兴趣的小伙伴可以了解下... 目录1. 网络数据接收2. 缓冲区管理(处理粘包/拆包)3. 常见数据格式解析3.1 jsON解析3.2 XML解析3.3 自定义

SpringMVC 通过ajax 前后端数据交互的实现方法

《SpringMVC通过ajax前后端数据交互的实现方法》:本文主要介绍SpringMVC通过ajax前后端数据交互的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价... 在前端的开发过程中,经常在html页面通过AJAX进行前后端数据的交互,SpringMVC的controll

Java中的工具类命名方法

《Java中的工具类命名方法》:本文主要介绍Java中的工具类究竟如何命名,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Java中的工具类究竟如何命名?先来几个例子几种命名方式的比较到底如何命名 ?总结Java中的工具类究竟如何命名?先来几个例子JD

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

python获取网页表格的多种方法汇总

《python获取网页表格的多种方法汇总》我们在网页上看到很多的表格,如果要获取里面的数据或者转化成其他格式,就需要将表格获取下来并进行整理,在Python中,获取网页表格的方法有多种,下面就跟随小编... 目录1. 使用Pandas的read_html2. 使用BeautifulSoup和pandas3.

Spring 中的循环引用问题解决方法

《Spring中的循环引用问题解决方法》:本文主要介绍Spring中的循环引用问题解决方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录什么是循环引用?循环依赖三级缓存解决循环依赖二级缓存三级缓存本章来聊聊Spring 中的循环引用问题该如何解决。这里聊

Java学习手册之Filter和Listener使用方法

《Java学习手册之Filter和Listener使用方法》:本文主要介绍Java学习手册之Filter和Listener使用方法的相关资料,Filter是一种拦截器,可以在请求到达Servl... 目录一、Filter(过滤器)1. Filter 的工作原理2. Filter 的配置与使用二、Listen

Pandas统计每行数据中的空值的方法示例

《Pandas统计每行数据中的空值的方法示例》处理缺失数据(NaN值)是一个非常常见的问题,本文主要介绍了Pandas统计每行数据中的空值的方法示例,具有一定的参考价值,感兴趣的可以了解一下... 目录什么是空值?为什么要统计空值?准备工作创建示例数据统计每行空值数量进一步分析www.chinasem.cn处