TabHost

TabWidget的ID必须设置为"@android:id/tabs",FrameLayout的ID必须设置为"@android:id/tabcontent"。如果MainActivity不是继承与TabActivity,那么通过findViewById()找到TabHost后,应该先执行一次setup()方法以完成TabHost的加载;如果MainActivity是继承与TabActivity,那么TabHost的ID就必须设置为"@android:id/tabhost"

public class MainActivity extends TabActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TabHost host = getTabHost();
        TabSpec spec;
        spec = host.newTabSpec("1").setIndicator("tab1").setContent(R.id.tv1);
        host.addTab(spec);
        spec = host.newTabSpec("2").setIndicator("tab2").setContent(R.id.tv2);
        host.addTab(spec);
        spec = host.newTabSpec("3").setIndicator("tab3").setContent(R.id.tv3);
        host.addTab(spec);
        host.setCurrentTab(0);
        host.setOnTabChangedListener(new OnTabChangeListener() {

            @Override
            public void onTabChanged(String tabId) {
                // TODO Auto-generated method stub
                Toast.makeText(getApplicationContext(), tabId,
                        Toast.LENGTH_SHORT).show();
            }
        });
    }

}

 


你可能感兴趣的:(TabHost)