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