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

相关文章

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. 使用

SpringBoot服务获取Pod当前IP的两种方案

《SpringBoot服务获取Pod当前IP的两种方案》在Kubernetes集群中,SpringBoot服务获取Pod当前IP的方案主要有两种,通过环境变量注入或通过Java代码动态获取网络接口IP... 目录方案一:通过 Kubernetes Downward API 注入环境变量原理步骤方案二:通过

Mybatis的分页实现方式

《Mybatis的分页实现方式》MyBatis的分页实现方式主要有以下几种,每种方式适用于不同的场景,且在性能、灵活性和代码侵入性上有所差异,对Mybatis的分页实现方式感兴趣的朋友一起看看吧... 目录​1. 原生 SQL 分页(物理分页)​​2. RowBounds 分页(逻辑分页)​​3. Page

Linux链表操作方式

《Linux链表操作方式》:本文主要介绍Linux链表操作方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、链表基础概念与内核链表优势二、内核链表结构与宏解析三、内核链表的优点四、用户态链表示例五、双向循环链表在内核中的实现优势六、典型应用场景七、调试技巧与

Linux实现线程同步的多种方式汇总

《Linux实现线程同步的多种方式汇总》本文详细介绍了Linux下线程同步的多种方法,包括互斥锁、自旋锁、信号量以及它们的使用示例,通过这些同步机制,可以解决线程安全问题,防止资源竞争导致的错误,示例... 目录什么是线程同步?一、互斥锁(单人洗手间规则)适用场景:特点:二、条件变量(咖啡厅取餐系统)工作流

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio