Andriod TabHost的使用TabHost

public class MainActivity extends TabActivity {
    private TabHost tabhost;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //从TabActivity上面获取放置Tab的TabHost
        tabhost = getTabHost();

        tabhost.addTab(tabhost
                //创建新标签one
                .newTabSpec("one")
                        //设置标签标题
                .setIndicator("红色")
                        //设置该标签的布局内容
                .setContent(R.id.widget_layout_red));

        tabhost.addTab(tabhost.newTabSpec("two").setIndicator("黄色").setContent(R.id.widget_layout_yellow));
    }
}


<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"//<span style="color:#FF0000;"><strong>TABHOST大容器</strong></span>
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@android:id/tabhost"
    >

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

        <TabWidget//<span style="color:#FF0000;"><strong>TAB头</strong></span>
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@android:id/tabs"
            ></TabWidget>

        <FrameLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:id="@android:id/tabcontent"  //<span style="color:#FF0000;"><strong>内容区域</strong></span>
            >
            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/widget_layout_red"
                android:background="#ff0000"
                android:orientation="vertical"
                ></LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/widget_layout_yellow"
                android:background="#FCD209"
                android:orientation="vertical"
                ></LinearLayout>

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

1.TabHost::含义就是整个Tab容器

2.TabSpec:Tab项的一个项对应一个TabSpec

3.setIndicator:Tab设置一个Tab项显示的View

4.TabWidget  就是Tab头,可以这么理解,可以把它放置在 FrameLayout放置在下面,Tab也就是置于页面底部了其实


感觉和fragemnt不知道比较好的结合,也算是一种基本的使用方法吧

你可能感兴趣的:(Andriod TabHost的使用TabHost)