TabHost

TabHost要重新定义布局,跟节点必须为<TabHost>

然后要有 <TabWidget
            android:id="@android:id/tabs"//ID必须为这个

     android:layout_width="match_parent"
            android:layout_height="wrap_content"

    />

只有要有<FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="match_parent"
            android:layout_height="match_parent" >

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

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >
       <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />
       

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

            <LinearLayout
                android:id="@+id/lin_btn"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <Button
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:text="我是按钮" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/lin_clock"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <AnalogClock
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content" />
            </LinearLayout>

            <LinearLayout
                android:id="@+id/lin_img"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:orientation="vertical" >

                <ImageView
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:src="@drawable/ic_launcher" />
            </LinearLayout>
        </FrameLayout>
      
    </LinearLayout>

</TabHost>
package com.example.tabdemo2;

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

public class MainActivity extends Activity {
    private TabHost tabhost;
    private int[] data = { R.id.lin_btn, R.id.lin_clock, R.id.lin_img };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tab);//这里注意布局
        tabhost = (TabHost) findViewById(R.id.tab);
        tabhost.setup();
        for (int i = 0; i < data.length; i++) {
            TabSpec tabs = tabhost.newTabSpec("tab"+i);// 定义一个TabSpec,给这个对象起一个名字
            tabs.setIndicator("标签"+i);// 给标签起名字用作显示
            tabs.setContent(data[i]);// spec绑定布局
            tabhost.addTab(tabs);// 把TabSpec加到tabhost里
        }
        tabhost.setCurrentTab(1);
        
    }

}
原文地址:https://www.cnblogs.com/84126858jmz/p/4897289.html