android tabhost使用方法

发布日期:2012-10-29 23:37:08

 

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" >

        <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" >

            </TabWidget>

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

                <LinearLayout
                    android:id="@+id/tab1"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >

                    <TextView
                        android:id="@+id/textView1"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView1" />

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab2"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                   
                                        <TextView
                        android:id="@+id/textView2"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView2" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/tab3"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                                       
                                                            <TextView
                        android:id="@+id/textView3"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView3" />
                </LinearLayout>
               
               
                                <LinearLayout
                    android:id="@+id/tab4"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent" >
                                       
                                                            <TextView
                        android:id="@+id/textView4"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:text="TextView4" />
                </LinearLayout>
               
               
            </FrameLayout>
        </LinearLayout>
    </TabHost>

</RelativeLayout>

 

 

 

 

 

 

 

 

package com.example.tabactivity;

import android.os.Bundle;
import android.app.Activity;
import android.app.TabActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TabHost;
import android.widget.TabWidget;
import android.widget.TextView;
import android.support.v4.app.NavUtils;

public class MainActivity extends TabActivity  {

 private TabHost tabHost;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       
        tabHost = getTabHost();   


        tabHost.setup();
        TabWidget tabWidget = tabHost.getTabWidget();

        tabHost.addTab(tabHost
                .newTabSpec("t1")
                .setIndicator("设置",this.getResources().getDrawable(R.drawable.p1))
                .setContent(R.id.tab1));

        tabHost.addTab(tabHost
                .newTabSpec("t2")
                .setIndicator("拍照",this.getResources().getDrawable(R.drawable.p2))
                .setContent(R.id.tab2));

        tabHost.addTab(tabHost
                .newTabSpec("t3")
                .setIndicator("摄影",this.getResources().getDrawable(R.drawable.p3))
                .setContent(R.id.tab3));
       
        tabHost.addTab(tabHost
                .newTabSpec("t4")
                .setIndicator("天气",this.getResources().getDrawable(R.drawable.p4))
                .setContent(R.id.tab4));

        // 设置单个tab的属性
        int count = tabWidget.getChildCount();
        for (int i = 0; i < count; i++) {
          View view = tabWidget.getChildTabViewAt(i);  
          //view.getLayoutParams().height = 80;
          final TextView tv = (TextView) view.findViewById(android.R.id.title);
          tv.setTextSize(10);
          //tv.setTextColor(this.getResources().getColorStateList(
          //  android.R.color.white));
        }
 
       
    }

 

   
}