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);  
 
    }
}
原文地址:https://www.cnblogs.com/tianzhijiexian/p/3860061.html