Android之TabHost布局 两种方式

2024-08-31 09:58

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

       第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。

      第二种方式,不继承TabActivity,在布局文件中定义TabHost即可,但是TabWidget的id必须是@android:id/tabs,FrameLayout的id必须是@android:id/tabcontent。

1、继承TabActivity

不多说,直接附上代码

在main.xml里的代码:

<?xml version="1.0" encoding="utf-8"?>
<!-- 第一种继承TabActivity,从TabActivity中用getTabHost()方法获取TabHost。各个Tab中的内容在布局文件中定义就行了。 -->
<!-- 定义TabHost组件 -->
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
    <!-- 定义第一个标签页的内容 -->

    <LinearLayout
        android:id="@+id/tab01"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <!-- 定义两个TextView用于显示标签页中的内容 -->

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="张三-2015-09-09  10:26" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="李四-2015-09-09  12:35" />
        
        <Button 
            android:id="@+id/btn_next"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="不继承TabActivity的方法"/>
    </LinearLayout>
    
    <!-- 定义第二个标签页的内容 -->
    <LinearLayout
        android:id="@+id/tab02"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <!-- 定义两个TextView用于显示标签页中的内容 -->
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="王五-2015-09-08  11:26" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="赵六-2015-09-09  13:18" />
    </LinearLayout>
    
    <!-- 定义第三个标签页的内容 -->
    <LinearLayout
        android:id="@+id/tab03"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <!-- 定义两个TextView用于显示标签页中的内容 -->

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="张三-2015-09-08  10:28" />

        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="赵六-2015-09-09  12:39" />
    </LinearLayout>
</TabHost>

在MainActivity里的代码:

package com.example.tabhosttest;
import android.R.color;
import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.content.Intent;
import android.graphics.Color;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.TabHost;

public class MainActivity extends TabActivity implements OnClickListener {
private Button btn_next;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//requestWindowFeature(Window.FEATURE_NO_TITLE);

//调用TabActivity的getTabHost()方法获取TabHost对象
TabHost tabHost = getTabHost();

//设置TabHost布局
LayoutInflater.from(this).inflate(R.layout.activity_main, tabHost.getTabContentView(), true);

// 设置一下TabHost的颜色
tabHost.setBackgroundColor(Color.argb(100, 22, 70, 100));

//添加第一个标签页
tabHost.addTab(tabHost.newTabSpec("tab01").setIndicator("已接电话").setContent(R.id.tab01));

//添加第二个标签页,并在其标签上添加图片
tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("未接电话", getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.tab02));
tabHost.addTab(tabHost.newTabSpec("tab02").setIndicator("123", getResources().getDrawable(R.drawable.gimp)).setContent(R.id.tab02));
//添加第三个标签页
tabHost.addTab(tabHost.newTabSpec("tab03").setIndicator("以拨电话").setContent(R.id.tab03));

btn_next = (Button) findViewById(R.id.btn_next);
btn_next.setOnClickListener(this);
}

@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;
}

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_next:
Intent intent = new Intent(this, SecondMethod.class);
startActivity(intent);
break;
}
}
}

运行结果如下图所示:


2、不继承TabActivity

在secondmethod.xml里的代码:

<?xml version="1.0" encoding="utf-8"?>
<!-- 继承普通Activity,<TabWidget>标签id必须为tabs、<FrameLayout>标签id必须为tabcontent.这个方式在通过findViewById获得TabHost之后,必须要调用setup方法。 -->
<!-- TabHost必须包含一个 TabWidget和一个FrameLayout -->
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical" >

        <!-- TabWidget 的id属性必须为 @android:id/tabs -->
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal" />
        <!-- FrameLayout的id属性必须为 @android:id/tabcontent -->

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >

            <TextView
                android:id="@+id/t_view1"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
            <EditText 
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="123"/>

            <TextView
                android:id="@+id/t_view2"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />

            <TextView
                android:id="@+id/t_view3"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent" />
        </FrameLayout>
    </LinearLayout>
</TabHost>

在SecondMethod.java里的代码:

package com.example.tabhosttest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TabHost;

public class SecondMethod extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.secondmethod);

//获取TabHost对象
TabHost tabHost = (TabHost) findViewById(R.id.tabhost);

//如果没有继承TabActivity时,通过这种方法加载启动tabHost
tabHost.setup();
tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("第一个标签", getResources().getDrawable(R.drawable.bg)).setContent(R.id.t_view1));
tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("第二个标签", getResources().getDrawable(R.drawable.gimp)).setContent(R.id.t_view2));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("第三个标签", getResources().getDrawable(R.drawable.ic_launcher)).setContent(R.id.t_view3));
}
}

运行效果如下图所示:


这篇关于Android之TabHost布局 两种方式的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



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

相关文章

C#读写文本文件的多种方式详解

《C#读写文本文件的多种方式详解》这篇文章主要为大家详细介绍了C#中各种常用的文件读写方式,包括文本文件,二进制文件、CSV文件、JSON文件等,有需要的小伙伴可以参考一下... 目录一、文本文件读写1. 使用 File 类的静态方法2. 使用 StreamReader 和 StreamWriter二、二进

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

k8s上运行的mysql、mariadb数据库的备份记录(支持x86和arm两种架构)

《k8s上运行的mysql、mariadb数据库的备份记录(支持x86和arm两种架构)》本文记录在K8s上运行的MySQL/MariaDB备份方案,通过工具容器执行mysqldump,结合定时任务实... 目录前言一、获取需要备份的数据库的信息二、备份步骤1.准备工作(X86)1.准备工作(arm)2.手

gitlab安装及邮箱配置和常用使用方式

《gitlab安装及邮箱配置和常用使用方式》:本文主要介绍gitlab安装及邮箱配置和常用使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1.安装GitLab2.配置GitLab邮件服务3.GitLab的账号注册邮箱验证及其分组4.gitlab分支和标签的

C++中零拷贝的多种实现方式

《C++中零拷贝的多种实现方式》本文主要介绍了C++中零拷贝的实现示例,旨在在减少数据在内存中的不必要复制,从而提高程序性能、降低内存使用并减少CPU消耗,零拷贝技术通过多种方式实现,下面就来了解一下... 目录一、C++中零拷贝技术的核心概念二、std::string_view 简介三、std::stri

Linux脚本(shell)的使用方式

《Linux脚本(shell)的使用方式》:本文主要介绍Linux脚本(shell)的使用方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录概述语法详解数学运算表达式Shell变量变量分类环境变量Shell内部变量自定义变量:定义、赋值自定义变量:引用、修改、删

python判断文件是否存在常用的几种方式

《python判断文件是否存在常用的几种方式》在Python中我们在读写文件之前,首先要做的事情就是判断文件是否存在,否则很容易发生错误的情况,:本文主要介绍python判断文件是否存在常用的几种... 目录1. 使用 os.path.exists()2. 使用 os.path.isfile()3. 使用