TabHost的初步使用

本文主要参考自:http://blog.csdn.net/wulianghuan/article/details/8588947 (里面有TabHost第二种定义的方式,继承TabActivity)

TabHost就是一个选项卡,类似于tab。这里我定义了三个标签,点击后会切换出不同的内容。之前用Fragment实现过类似的效果,总觉得还是Fragment+tab好用点。

activity_main.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TabHost
        android:id="@+id/tablehost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

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

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

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

            </FrameLayout>
        </LinearLayout>
    </TabHost>

</LinearLayout>
复制代码

每个选项卡点击后的内容的布局。其实就是在上面定义的FrameLayout中的内容

home.xml

复制代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="match_parent"
     android:layout_height="fill_parent"
     android:background="#0000FF"
     android:id="@+id/home"
     android:orientation="vertical">
    
    <TextView
         android:layout_width="fill_parent"
         android:layout_height="wrap_content"
         android:text="home"
    />
    
</LinearLayout>
复制代码

 

comment.xml / save.xml布局和home一样。

下面是java代码——MainActivity.java

复制代码
package com.example.tabhost;

import android.app.Activity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //取得TabHost  
        TabHost tabhost = (TabHost) findViewById(R.id.tablehost);  
        tabhost.setup();  
        LayoutInflater inflater = LayoutInflater.from(this);  
        //把配置文件转换为显示TabHost内容的FrameLayout中的层级  
        inflater.inflate(R.layout.home, tabhost.getTabContentView());  
        inflater.inflate(R.layout.comment, tabhost.getTabContentView());  
        inflater.inflate(R.layout.save, tabhost.getTabContentView());  
 
        //设置HOME标签    
        TabSpec spec1 = tabhost.newTabSpec("HOME").
                setIndicator("HOME");  // setIndicator设置标题
        //设置HOME模块显示内容  
        spec1.setContent(R.id.home);  
        tabhost.addTab(spec1);  
          
        TabSpec spec2 = tabhost.newTabSpec("COMMENT").setIndicator("COMMENT");  
        spec2.setContent(R.id.comment);  
        tabhost.addTab(spec2);  
          
        TabSpec spec3 = tabhost.newTabSpec("SAVE").setIndicator("SAVE");  
        spec3.setContent(R.id.save);  
        tabhost.addTab(spec3);  
 
    }
}
复制代码
posted @   developer_Kale  阅读(404)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· DeepSeek如何颠覆传统软件测试?测试工程师会被淘汰吗?
网站流量统计工具
点击右上角即可分享
微信分享提示