tabtag 在顶部布局文件:mytesttabtop.xml
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" 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>
tabtag 在底部布局文件:mytesttabbottom.xml
<?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tabhost" android:layout_width="fill_parent" android:layout_height="fill_parent"> <RelativeLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TabWidget android:id="@android:id/tabs" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="fill_parent" android:layout_height="fill_parent" android:layout_above="@android:id/tabs"> </FrameLayout> </RelativeLayout> </TabHost>
自定义TAB显示view
public class TabTagView extends TextView { public TabTagView(Context context, String label) { super(context); setText(label); setGravity(Gravity.CENTER); setBackgroundResource(R.drawable.tab_bg_selector); } /** * * @param context * @param label * @param index 图标绘制位置:1在文本左侧、2在文本上端、3在文本右侧、4在文本下端 */ public TabTagView(Context context, String label, int index) { super(context); setText(label); setGravity(Gravity.CENTER); switch (index) { case 1: setCompoundDrawablesWithIntrinsicBounds(R.drawable.star , 0, 0, 0); break; case 2: setCompoundDrawablesWithIntrinsicBounds(0,R.drawable.star , 0, 0); break; case 3: setCompoundDrawablesWithIntrinsicBounds(0 , 0, R.drawable.star , 0); break; case 4: setCompoundDrawablesWithIntrinsicBounds(0 , 0, 0, R.drawable.star ); break; default: break; } setBackgroundResource(R.drawable.tab_bg_selector); } }
tab_bg_selector.xml
<?xml version="1.0" encoding="UTF-8"?> <selector xmlns:android="http://schemas.android.com/apk/res/android"> <item android:state_pressed="true" android:drawable="@drawable/tab_pressed" /> <!-- pressed --> <item android:state_selected="true" android:drawable="@drawable/tab_pressed" /> <!-- selected --> <item android:drawable="@drawable/tab_normal" /> <!-- default --> </selector
Activity
public class TestTab extends Activity { TabHost mTabHost; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.mytesttab); mTabHost = (TabHost) findViewById(R.id.include02); mTabHost.setup(); LayoutInflater inflater_tab1 = LayoutInflater.from(this); inflater_tab1.inflate(R.layout.tab1, mTabHost.getTabContentView()); inflater_tab1.inflate(R.layout.tab2, mTabHost.getTabContentView()); inflater_tab1.inflate(R.layout.tab3, mTabHost.getTabContentView()); mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator(new TabTagView(this, "TAB 1")) .setContent(R.id.mytab1)); mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator(new TabTagView(this, "TAB 2",1)) .setContent(R.id.mytab2)); mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator(new TabTagView(this, "TAB 3",3)) .setContent(R.id.mytab3)); } }