Android TabActivity——学习笔记

wuqiong 2010-09-24

1、通过布局指定要显示的内容:

tabs.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <TextView android:id="@+id/view1"
        android:background="@drawable/blue"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/tabs_1_tab_1"/>
    <TextView android:id="@+id/view2"
        android:background="@drawable/red"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/tabs_1_tab_2"/>
    <TextView android:id="@+id/view3"
        android:background="@drawable/green"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:text="@string/tabs_1_tab_3"/>
</FrameLayout>

Tabs1.java

public class Tabs1 extends TabActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TabHost tabHost = getTabHost();
        
        LayoutInflater.from(this).inflate(R.layout.tabs1, tabHost.getTabContentView(), true);
        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("tab1")
                .setContent(R.id.view1));
        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("tab2")
                .setContent(R.id.view2));
        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("tab3")
                .setContent(R.id.view3));
    }
}

2、通过TabHost.TabContentFactor指定布局

public class Tabs2 extends TabActivity implements TabHost.TabContentFactory {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        final TabHost tabHost = getTabHost();
        tabHost.addTab(tabHost.newTabSpec("tab1")
                .setIndicator("tab1", getResources().getDrawable(R.drawable.star_big_on))
                .setContent(this));
        tabHost.addTab(tabHost.newTabSpec("tab2")
                .setIndicator("tab2")
                .setContent(this));
        tabHost.addTab(tabHost.newTabSpec("tab3")
                .setIndicator("tab3")
                .setContent(this));
    }
    /** {@inheritDoc} */
    public View createTabContent(String tag) {
        final TextView tv = new TextView(this);
        tv.setText("Content for tab with tag " + tag);
        return tv;
    }
}

3、通过Intent来启动其他的Activity

public class Tabs3 extends TabActivity {   
    @Override  
    protected void onCreate(Bundle savedInstanceState) {   
        super.onCreate(savedInstanceState);   
        final TabHost tabHost = getTabHost();   
        tabHost.addTab(tabHost.newTabSpec("tab1")   
                .setIndicator("list")   
                .setContent(new Intent(this, List1.class)));   
        tabHost.addTab(tabHost.newTabSpec("tab2")   
                .setIndicator("photo list")   
                .setContent(new Intent(this, List8.class)));   
           
        // This tab sets the intent flag so that it is recreated each time   
        // the tab is clicked.   
        tabHost.addTab(tabHost.newTabSpec("tab3")   
                .setIndicator("destroy")   
                .setContent(new Intent(this, Controls2.class)   
                        .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));   
    }   
}

相关推荐