android用户界面-组件Widget-选项卡Tab
使用Tab的步骤: 1、在布局文件中使用FrameLayout列出Tab组件及Tab中的内容组件。 2、Activity要继承TabActivity。 3、调用TabActivity的getTabHost()方法获得TabHost对象。 4、通过TabHost创建Tab选项。 /Chapter04_UI_Tab01/src/com/amaker/test/MainActivity.java 代码 packagecom.amaker.test; importandroid.app.TabActivity; importandroid.os.Bundle; importandroid.view.LayoutInflater; importandroid.view.Window; importandroid.view.WindowManager; importandroid.widget.TabHost; importandroid.widget.Toast; importandroid.widget.TabHost.OnTabChangeListener; publicclassMainActivityextendsTabActivity{ @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); /*requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);*/ TabHostth=getTabHost(); LayoutInflater.from(this).inflate(R.layout.main,th.getTabContentView(),true); th.addTab(th.newTabSpec("all").setIndicator("所有通话记录").setContent(R.id.TextView01)); th.addTab(th.newTabSpec("ok").setIndicator("已接来电").setContent(R.id.TextView02)); th.addTab(th.newTabSpec("cancel").setIndicator("未接来电").setContent(R.id.TextView03)); th.setOnTabChangedListener( newOnTabChangeListener(){ @Override publicvoidonTabChanged(StringtabId){ Toast.makeText(MainActivity.this,tabId,Toast.LENGTH_LONG).show(); } } ); } } /Chapter04_UI_Tab01/res/layout/main.xml 代码 <?xmlversion="1.0"encoding="utf-8"?> <FrameLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/FrameLayout01" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="所有通话记录"></TextView> <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="已接来电"></TextView> <TextView android:id="@+id/TextView03" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="未接来电"></TextView> </FrameLayout> 通过实现一个接口TabHost.TabContentFactory的createTabContent方法来制定Tab的内容 /Chapter04_UI_Tab02/src/com/amaker/test/MainActivity.java 代码 packagecom.amaker.test; importjava.util.ArrayList; importjava.util.List; importandroid.app.TabActivity; importandroid.os.Bundle; importandroid.view.View; importandroid.widget.ArrayAdapter; importandroid.widget.ListView; importandroid.widget.TabHost; publicclassMainActivityextendsTabActivityimplements TabHost.TabContentFactory{ /**Calledwhentheactivityisfirstcreated.*/ @Override publicvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); TabHostth=getTabHost(); th.addTab(th.newTabSpec("all").setIndicator("所有通话记录").setContent(this)); th.addTab(th.newTabSpec("ok").setIndicator("已接来电").setContent(this)); th .addTab(th.newTabSpec("cancel").setIndicator("未接来电") .setContent(this)); } publicViewcreateTabContent(Stringtag){ ListViewlv=newListView(this); List<String>list=newArrayList<String>(); list.add(tag); if(tag.equals("all")){ list.add("tom"); list.add("kite"); list.add("rose"); }elseif(tag.equals("ok")){ list.add("tom"); list.add("kite"); }else{ list.add("rose"); } ArrayAdapteradapter=newArrayAdapter(this, android.R.layout.simple_list_item_checked,list); lv.setAdapter(adapter); returnlv; } } /Chapter04_UI_Tab02/res/layout/main.xml 代码 <?xmlversion="1.0"encoding="utf-8"?> <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> </LinearLayout> 本文转自linzheng 51CTO博客,原文链接:http://blog.51cto.com/linzheng/1080696