tab使用 TabActivity TabHost Tabspec常用方法
16lz
2021-01-25
本文是参考Android官方提供的sample里面的ApiDemos的学习总结。
TabActivity
首先Android里面有个名为TabActivity来给我们方便使用。其中有以下可以关注的函数: public TabHost getTabHost ()获得当前TabActivity的TabHost public TabWidget getTabWidget ()获得当前TabActivity的TabWidget public void setDefaultTab (String tag)这两个函数很易懂,就是设置默认的Tab public void setDefaultTab (int index)通过tab名——tag或者index(从0开始) protected void onRestoreInstanceState (Bundle state)这两个函数的介绍可以 protected void onSaveInstanceState (Bundle outState)参考 Activity的生命周期TabHost
那么我们要用到的Tab载体是TabHost,需要从TabActivity.getTabHost获取。 现在看看TabHost类,它有3个内嵌类:1个类TabHost.TabSpec,2个接口TabHost.TabContentFactory和TabHost.OnTabChangeListener。后面会介绍这些类和接口。 TabHost类的一些函数: public void addTab (TabHost.TabSpec tabSpec)添加tab,参数TabHost.TabSpec通过下面的函数返回得到 public TabHost.TabSpec newTabSpec (String tag)创建TabHost.TabSpec public void clearAllTabs ()remove所有的Tabs public int getCurrentTab () public String getCurrentTabTag () public View getCurrentTabView () public View getCurrentView () public FrameLayout getTabContentView ()返回Tab content的FrameLayout public TabWidget getTabWidget () public void setCurrentTab (int index) 设置当前的Tab by index public void setCurrentTabByTag (String tag)设置当前的Tab by tag public void setOnTabChangedListener (TabHost.OnTabChangeListener l)设置TabChanged事件的响应处理 public void setup ()这个函数后面介绍TabHost.TabSpec
从上面的函数可以知道如何添加tab了,要注意,这里的Tag(标签),不是Tab按钮上的文字。 而要设置tab的label和content,需要设置TabHost.TabSpec类。引用SDK里面的话——“A tab has a tab indicator, content, and a tag that is used to keep track of it.”,TabHost.TabSpec就是管理这3个东西: public String getTag () public TabHost.TabSpec setContent public TabHost.TabSpec setIndicator 我理解这里的 Indicator就是Tab上的label,它可以 设置label: setIndicator (CharSequence label) 或者同时 设置label和icon: setIndicator (CharSequence label, Drawable icon) 或者直接 指定某个view: setIndicator (View view) 对于 Content,就是Tab里面的内容,可以 设置View的id: setContent(int viewId) 或者 TabHost.TabContentFactory的createTabContent(String tag)来处理: setContent(TabHost.TabContentFactory contentFactory) 或者用 new Intent来引入其他Activity的内容: setContent(Intent intent) 现在来看官方的Views/Tabs/Content By Id例子: 代码- publicclassTabs1extendsTabActivity{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- TabHosttabHost=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));
- }
- }
- <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <TextViewandroid:id="@+id/view1"
- android:background="@drawable/blue"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="@string/tabs_1_tab_1"/>
- <TextViewandroid:id="@+id/view2"
- android:background="@drawable/red"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:text="@string/tabs_1_tab_2"/>
- <TextViewandroid: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>
- <!--strings.xml
- <stringname="tabs_1_tab_1">tab1</string>
- <stringname="tabs_1_tab_2">tab2</string>
- <stringname="tabs_1_tab_3">tab3</string>
- -->
- publicclassTabs2extendsTabActivityimplementsTabHost.TabContentFactory{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- finalTabHosttabHost=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));
- }
- publicViewcreateTabContent(Stringtag){
- finalTextViewtv=newTextView(this);
- tv.setText("Contentfortabwithtag"+tag);
- returntv;
- }
- }
- publicclassTabs3extendsTabActivity{
- @Override
- protectedvoidonCreate(BundlesavedInstanceState){
- super.onCreate(savedInstanceState);
- finalTabHosttabHost=getTabHost();
- tabHost.addTab(tabHost.newTabSpec("tab1")
- .setIndicator("list")
- .setContent(newIntent(this,List1.class)));
- tabHost.addTab(tabHost.newTabSpec("tab2")
- .setIndicator("photolist")
- .setContent(newIntent(this,List8.class)));
- //Thistabsetstheintentflagsothatitisrecreatedeachtime
- //thetabisclicked.
- tabHost.addTab(tabHost.newTabSpec("tab3")
- .setIndicator("destroy")
- .setContent(newIntent(this,Controls2.class)
- .addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)));
- }
- }
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 usingsetContent(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.本文出自 “学习Android” 博客,请务必保留此出处http://android.blog.51cto.com/268543/315208
更多相关文章
- android JNI 多线程 C函数回调
- Android(安卓)读取扫码枪的内容,可以读取条形码 ,二维码
- Android(安卓)init初始化程序分析
- android wifi ralink rt3070开发记录
- android各种控件的事件监听及举例
- android输入法01:SoftKeyboard源码解析03
- Android属性服务分析(property service)
- android给 TextView 加上效果和事件响应 判断是否点击函数
- 深入源码分析non-sdk并绕过Android(安卓)9.0反射限制