先实现入口activity : ACTMainMenu.java
public class ACTMainMenu extends FragmentActivity { private FragmentTabHost mTabHost; private RadioGroup m_radioGroup; String tabs[] = { "Tab1", "Tab2" }; Class<?> cls[] = { FMTab1.class, FMTab2.class }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.widget_tabs); init(); } private void init() { mTabHost = (FragmentTabHost) this.findViewById(android.R.id.tabhost); mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent); mTabHost.getTabWidget().setVisibility(View.GONE); for (int i = 0; i < tabs.length; i++) { mTabHost.addTab(mTabHost.newTabSpec(tabs[i]).setIndicator(tabs[i]), cls[i], null); } m_radioGroup = (RadioGroup) findViewById(R.id.main_radiogroup); m_radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.RadioButton0: mTabHost.setCurrentTabByTag(tabs[0]); break; case R.id.RadioButton1: mTabHost.setCurrentTabByTag(tabs[1]); break; } } }); ((RadioButton) m_radioGroup.getChildAt(0)).toggle(); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <RadioGroup android:id="@+id/main_radiogroup" android:layout_width="fill_parent" android:layout_height="50dp" android:layout_alignParentBottom="true" android:orientation="horizontal" > <com.zoeice.tabdemo.CVRadioButton android:id="@+id/RadioButton0" style="@style/tab_item_background" android:drawableTop="@drawable/tab_item1_selector" android:text="@string/STR_TAB_01" android:textColor="#FFF" /> <com.zoeice.tabdemo.CVRadioButton android:id="@+id/RadioButton1" style="@style/tab_item_background" android:drawableTop="@drawable/tab_item2_selector" android:text="@string/STR_TAB_02" android:textColor="#FFF" /> </RadioGroup> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@id/main_radiogroup" android:background="#b2b2b2" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <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="match_parent" android:layout_height="0dp" android:layout_weight="1" android:visibility="gone" /> <FrameLayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> </android.support.v4.app.FragmentTabHost> </RelativeLayout>
FMTab2.java类似。
public class FMTab1 extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View layoutView = inflater.inflate(R.layout.fm_tab1, null); return layoutView; } }
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:background="#789"> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Tab 1" android:textSize="40sp" android:textColor="@android:color/white" /> </LinearLayout>
public class CVRadioButton extends RadioButton { public CVRadioButton(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); } public CVRadioButton(Context context, AttributeSet attrs) { super(context, attrs); } public CVRadioButton(Context context) { super(context); } @Override protected void onDraw(Canvas canvas) { Drawable[] drawables = getCompoundDrawables(); if (drawables != null) { Drawable drawableLeft = drawables[0]; Drawable drawableTop = drawables[1]; Drawable drawableRight = drawables[2]; Drawable drawableBottom = drawables[3]; if (drawableLeft != null || drawableRight != null) { float textWidth = getPaint().measureText(getText().toString()); int drawablePadding = getCompoundDrawablePadding(); int drawableWidth = 0; if (drawableLeft != null) drawableWidth = drawableLeft.getIntrinsicWidth(); else if (drawableRight != null) { drawableWidth = drawableRight.getIntrinsicWidth(); } float bodyWidth = textWidth + drawableWidth + drawablePadding; canvas.translate((getWidth() - bodyWidth) / 2, 0); } if (drawableTop != null || drawableBottom != null) { float textHeight = getPaint().getTextSize(); int drawablePadding = getCompoundDrawablePadding(); int drawableHeight = 0; if (drawableTop != null) drawableHeight = drawableTop.getIntrinsicHeight(); else if (drawableBottom != null) { drawableHeight = drawableBottom.getIntrinsicHeight(); } float bodyHeight = textHeight + drawableHeight + drawablePadding; canvas.translate(0, (getHeight() - bodyHeight) / 2); } } super.onDraw(canvas); } }
<style name="tab_item_background"> <item name="android:textAppearance">@style/tab_item_text_style</item> <item name="android:gravity">center_horizontal</item> <item name="android:background">@drawable/tab_bg_selector</item> <item name="android:layout_width">match_parent</item> <item name="android:layout_height">match_parent</item> <item name="android:button">@null</item> <item name="android:drawablePadding">3.0dip</item> <item name="android:layout_weight">1.0</item> </style>
源代码:http://download.csdn.net/detail/zoeice/7933873