TabHost是多標籤控制項,包含多個Tab,可在一個頁面顯示多種布局。
使用方法一:
建立Activity繼承TabActivity,利用其方法產生TabHost
TabUse1.java
package org.tabhost;<br />import org.tabhost.R;<br />import android.app.TabActivity;<br />import android.os.Bundle;<br />import android.view.LayoutInflater;<br />import android.widget.*;<br />public class TabUse1 extends TabActivity {<br /> private TabHost tabhost;<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> tabhost=getTabHost();<br /> LayoutInflater layoutInflater=LayoutInflater.from(this);</p><p> layoutInflater.inflate(R.layout.tab1, tabhost.getTabContentView());<br /> FrameLayout frameLayout=(FrameLayout) findViewById(R.id.tab1);<br /> TextView textView=new TextView(this);<br /> textView.setText("tab1-TextView");<br /> frameLayout.addView(textView);<br /> TabHost.TabSpec tabSpec1=tabhost.newTabSpec("tab-1");<br /> tabSpec1.setIndicator("tab-1", null);<br /> tabSpec1.setContent(R.id.tab1);<br /> tabhost.addTab(tabSpec1);</p><p> layoutInflater.inflate(R.layout.tab2, tabhost.getTabContentView());<br /> LinearLayout lineLayout=(LinearLayout) findViewById(R.id.tab2);<br /> Button btn=new Button(this);<br /> btn.setText("tab2-Button");<br /> lineLayout.addView(btn);<br /> TabHost.TabSpec tabSpec2=tabhost.newTabSpec("tab-2");<br /> tabSpec2.setIndicator("tab-2", null);<br /> tabSpec2.setContent(R.id.tab2);<br /> tabhost.addTab(tabSpec2);<br /> }<br />}
tab1.xml
<?xml version="1.0" encoding="UTF-8"?><br /><FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"<br /> android:id="@+id/tab1"<br /> ><br /></FrameLayout>
tab2.xml
<?xml version="1.0" encoding="UTF-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_height="fill_parent"<br /> android:layout_width="fill_parent"<br /> android:id="@+id/tab2"><br /></LinearLayout>
方法二:
通過介面定義檔案產生TabHost
TabTest.java
package org.tabhost;<br />import android.app.Activity;<br />import android.os.Bundle;<br />import android.widget.TabHost;<br />public class TabTest extends Activity {<br /> /** Called when the activity is first created. */<br />/** Called when the activity is first created. */<br /> private TabHost tabHost; </p><p> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.main);<br /> try{<br /> tabHost = (TabHost) this.findViewById(R.id.TabHost01);<br /> tabHost.setup(); </p><p> tabHost.addTab(tabHost.newTabSpec("tab_1")<br /> .setContent(new TabFactory(this))<br /> .setIndicator("TAB1"));<br /> tabHost.addTab(tabHost.newTabSpec("tab_2")<br /> .setContent(new TabFactory(this))<br /> .setIndicator("TAB2"));<br /> tabHost.addTab(tabHost.newTabSpec("tab_3")<br /> .setContent(new TabFactory(this))<br /> .setIndicator("TAB3"));<br /> tabHost.setCurrentTab(1);<br /> }catch(Exception ex){<br /> ex.printStackTrace();<br /> } </p><p> }
TabFactory.java,在此類中可以動態產生各Tab中的控制項
package org.tabhost;<br />import android.content.Context;<br />import android.view.View;<br />import android.widget.EditText;<br />import android.widget.TabHost.TabContentFactory;<br />public class TabFactory implements TabContentFactory {<br />private Context con;<br />public TabFactory(Context c){<br />con=c;<br />}<br />@Override<br />public View createTabContent(String arg0) {<br />EditText text=new EditText(con);<br />text.setText("text1");<br />return text;<br />}<br />}
main.xml
<?xml version="1.0" encoding="utf-8"?><br /><TabHost xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:id="@+id/TabHost01" android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"><br /> <LinearLayout android:layout_width="fill_parent"<br /> android:orientation="vertical" android:layout_height="fill_parent"><br /> <TabWidget android:id="@android:id/tabs"<br /> android:layout_width="fill_parent"<br /> android:layout_height="wrap_content" /><br /> <FrameLayout android:id="@android:id/tabcontent"<br /> android:layout_width="fill_parent"<br /> android:layout_height="fill_parent"> </p><p> </FrameLayout><br /> </LinearLayout><br /></TabHost>