TabHost的使用

package cn.itcast.tabhost;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
import android.widget.TabHost.TabSpec;
public class TabHostActivity extends TabActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TabHost tabhost = getTabHost();
                
        TabSpec  spec1 = tabhost.newTabSpec("便籤1");
        TabSpec  spec2 = tabhost.newTabSpec("便籤2");
        TabSpec  spec3 = tabhost.newTabSpec("便籤3");
        spec1.setIndicator("標籤1", getResources().getDrawable(R.drawable.ic_launcher));
        spec2.setIndicator("標籤2", getResources().getDrawable(R.drawable.ic_launcher));
        spec3.setIndicator("標籤3", getResources().getDrawable(R.drawable.ic_launcher));
        Intent intent1 = new Intent(this,Activity01.class);
        spec1.setContent(intent1);
        Intent intent2 = new Intent(this,Activity02.class);
        spec2.setContent(intent2);
        Intent intent3 = new Intent(this,Activity03.class);
        spec3.setContent(intent3);
                
        tabhost.addTab(spec1);
        tabhost.addTab(spec2);
        tabhost.addTab(spec3);
    }
}
<?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="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
        <RelativeLayout
            android:layout_width="fill_parent"
            android:layout_height="fill_parent" >
            <TabWidget
                android:layout_alignParentBottom="true"
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >
            </TabWidget>
            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:layout_marginBottom="60dip" >
            </FrameLayout>
        </RelativeLayout>
    </TabHost>
</LinearLayout>
package cn.itcast.tabhost;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Activity01 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        TextView tv = new TextView(this);
        tv.setTextSize(30);
        tv.setText("activity01");
        // TODO Auto-generated method stub
        setContentView(tv);
        super.onCreate(savedInstanceState);
    }
}
package cn.itcast.tabhost;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Activity02 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        TextView tv = new TextView(this);
        tv.setTextSize(30);
        tv.setText("activity02");
        // TODO Auto-generated method stub
        setContentView(tv);
        super.onCreate(savedInstanceState);
    }
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章