TabHost卡片的使用

    如果在屏幕上要放置很多的控件,可能一个屏幕放不下,出来使用滚动视频的方式外,还以使用标签控件对屏幕进行分页显示,当单击标签控件的不同标签时,会显示当前标签的内容,在Android系统中一个标签可以是一个View或者Activity。

    TabHost是标签空间的核心类,也是一个标签的集合,每一个标签是TabHost.TabSpec对象。通过TabHost类的addTab的方法添加多个TabHost.TabSpec对象。

一、建立工程,如图

二、tabhost.xml中代码(本例中没有用到activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >

    <LinearLayout 
        android:id="@+id/ly1"
          android:layout_width="fill_parent"
         android:layout_height="fill_parent">
         
         <TextView 
             android:id="@+id/tab1_txt"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="the content of tab1_txt"/>
    </LinearLayout>
 
    <LinearLayout
          android:id="@+id/ly2"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
         
         <TextView 
             android:id="@+id/tab2_txt"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="the content of tab2_txt"/>
    </LinearLayout>
    
    <LinearLayout
          android:id="@+id/ly3"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
         
         <TextView 
             android:id="@+id/tab3_txt"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:text="the content of tab03_txt"/>
    </LinearLayout>

</FrameLayout>
View Code

三、MainActivity.java中代码

package com.study.tabhost;

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

public class MainActivity extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
      //我們繼承TabActivity,tabActivity裏面已經有一個TabHost對象,我們就直接通過getTabHost()獲取
              TabHost tabHost  = this.getTabHost();
              //把自己的佈局文件添加到tabhost裏面,tabhost相當於一個viewroot
              LayoutInflater.from(this).inflate(R.layout.tabhost, tabHost.getTabContentView(), true);

              TabSpec tabSpec;
              //第一个tab
              tabSpec = tabHost.newTabSpec("tab1").setIndicator("菜单1", this.getResources().getDrawable(R.drawable.item1))
                      .setContent(R.id.tab1_txt);    
              tabHost.addTab(tabSpec);
              
              //第二个tab
              tabSpec = tabHost.newTabSpec("tab2").setIndicator("菜单2", this.getResources().getDrawable(R.drawable.item2))
                      .setContent(R.id.tab2_txt); 
              tabHost.addTab(tabSpec);
              
              //第三个tab
              tabSpec = tabHost.newTabSpec("tab3").setIndicator("菜单3", this.getResources().getDrawable(R.drawable.item3))
                      .setContent(R.id.tab3_txt);
              tabHost.addTab(tabSpec);

              //設置第一次打開時默認顯示的tab,參數與tabHost.newTabSpec("tab1")的參數相同
              tabHost.setCurrentTabByTag("tab2");
              //設置第一次打開時默認顯示的tab,參數是其添加到標籤中的順序,tab的位置使從0開始的。
              //tabHost.setCurrentTab(1);      
    }


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

四、效果图

原文地址:https://www.cnblogs.com/kingshow123/p/tabhost.html