Android UI学习之Tab的学习和使用
2010-05-26 15:57:00 来源:WEB开发网至于Views/Tabs/Content By Intent例子,就只是贴出代码,不给截图了:
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))); } }
效果:Tab1的内容是List1的Activity,Tab2的是List8的Activity,Tab3的是controls2.Activity。
TabHost.OnTabChangeListener
TabHost.OnTabChangeListener接口只有一个抽象方法onTabChanged(String tagString),明显地,在onTabChanged(String tagString)方法里面swtich..case..来判断tagString分别处理就行了。
TabHost.setup()
在此贴出SDK doc里面的相关解释:
public void setup () Since: API Level 1
Call setup() before adding tabs if loading TabHost using findViewById(). However,You do not need to call setup() after getTabHost() in TabActivity. Example:
mTabHost = (TabHost)findViewById(R.id.tabhost);
mTabHost.setup();
mTabHost.addTab(TAB_TAG_1, "Hello, world!", "Tab 1");
//我的理解是,如果要用到findViewById来获取TabHost,然后add tabs的话,需要在addTab前call setup();
public void setup (LocalActivityManager activityGroup) Since: API Level 1
If you are using setContent(android.content.Intent), this must be called since the activityGroup is needed to launch the local activity. This is done for you if you extend TabActivity.
Parameters
activityGroup Used to launch activities for tab content.
更多精彩
赞助商链接