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

相关文章

Spring IOC的三种实现方式详解

《SpringIOC的三种实现方式详解》:本文主要介绍SpringIOC的三种实现方式,在Spring框架中,IOC通过依赖注入来实现,而依赖注入主要有三种实现方式,构造器注入、Setter注入... 目录1. 构造器注入(Cons编程tructor Injection)2. Setter注入(Setter

Android kotlin语言实现删除文件的解决方案

《Androidkotlin语言实现删除文件的解决方案》:本文主要介绍Androidkotlin语言实现删除文件的解决方案,在项目开发过程中,尤其是需要跨平台协作的项目,那么删除用户指定的文件的... 目录一、前言二、适用环境三、模板内容1.权限申请2.Activity中的模板一、前言在项目开发过程中,尤

Java中List转Map的几种具体实现方式和特点

《Java中List转Map的几种具体实现方式和特点》:本文主要介绍几种常用的List转Map的方式,包括使用for循环遍历、Java8StreamAPI、ApacheCommonsCollect... 目录前言1、使用for循环遍历:2、Java8 Stream API:3、Apache Commons

虚拟机与物理机的文件共享方式

《虚拟机与物理机的文件共享方式》文章介绍了如何在KaliLinux虚拟机中实现物理机文件夹的直接挂载,以便在虚拟机中方便地读取和使用物理机上的文件,通过设置和配置,可以实现临时挂载和永久挂载,并提供... 目录虚拟机与物理机的文件共享1 虚拟机设置2 验证Kali下分享文件夹功能是否启用3 创建挂载目录4

linux报错INFO:task xxxxxx:634 blocked for more than 120 seconds.三种解决方式

《linux报错INFO:taskxxxxxx:634blockedformorethan120seconds.三种解决方式》文章描述了一个Linux最小系统运行时出现的“hung_ta... 目录1.问题描述2.解决办法2.1 缩小文件系统缓存大小2.2 修改系统IO调度策略2.3 取消120秒时间限制3

Linux alias的三种使用场景方式

《Linuxalias的三种使用场景方式》文章介绍了Linux中`alias`命令的三种使用场景:临时别名、用户级别别名和系统级别别名,临时别名仅在当前终端有效,用户级别别名在当前用户下所有终端有效... 目录linux alias三种使用场景一次性适用于当前用户全局生效,所有用户都可调用删除总结Linux

Mybatis官方生成器的使用方式

《Mybatis官方生成器的使用方式》本文详细介绍了MyBatisGenerator(MBG)的使用方法,通过实际代码示例展示了如何配置Maven插件来自动化生成MyBatis项目所需的实体类、Map... 目录1. MyBATis Generator 简介2. MyBatis Generator 的功能3

Python数据处理之导入导出Excel数据方式

《Python数据处理之导入导出Excel数据方式》Python是Excel数据处理的绝佳工具,通过Pandas和Openpyxl等库可以实现数据的导入、导出和自动化处理,从基础的数据读取和清洗到复杂... 目录python导入导出Excel数据开启数据之旅:为什么Python是Excel数据处理的最佳拍档

SpringBoot项目启动后自动加载系统配置的多种实现方式

《SpringBoot项目启动后自动加载系统配置的多种实现方式》:本文主要介绍SpringBoot项目启动后自动加载系统配置的多种实现方式,并通过代码示例讲解的非常详细,对大家的学习或工作有一定的... 目录1. 使用 CommandLineRunner实现方式:2. 使用 ApplicationRunne

VUE动态绑定class类的三种常用方式及适用场景详解

《VUE动态绑定class类的三种常用方式及适用场景详解》文章介绍了在实际开发中动态绑定class的三种常见情况及其解决方案,包括根据不同的返回值渲染不同的class样式、给模块添加基础样式以及根据设... 目录前言1.动态选择class样式(对象添加:情景一)2.动态添加一个class样式(字符串添加:情