如今網上通常的實現方式就是用radoigroup或者fragmentLayout兩種方式來實現tabhost,一下我分別來介紹這兩種方式。 java
主佈局文件:main_fragment_tabs.xml android
<?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.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" 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" /> <!-- 設置每一個選項卡之間比列都爲1,設置tabhost自帶選擇卡不可見 --> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:visibility="gone" /> <!-- 設置設置內容權重爲1 --> <FrameLayout android:id="@+id/realtabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" /> </LinearLayout> <!-- 以RadioGroup代替tabhost選項卡 --> <RadioGroup android:id="@+id/main_radiogroup" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:background="@drawable/tab_widget_background" android:gravity="center_vertical" android:orientation="horizontal" android:padding="2dip" > <RadioButton android:id="@+id/RadioButton0" style="@style/tab_item_background" android:drawableTop="@drawable/tab_icon1" android:text="主頁" /> <RadioButton android:id="@+id/RadioButton1" style="@style/tab_item_background" android:drawableTop="@drawable/tab_icon2" android:text="關於" /> <RadioButton android:id="@+id/RadioButton2" style="@style/tab_item_background" android:drawableTop="@drawable/tab_icon3" android:text="設置" /> <RadioButton android:id="@+id/RadioButton3" style="@style/tab_item_background" android:drawableTop="@drawable/tab_icon4" android:text="搜索" /> <RadioButton android:id="@+id/RadioButton4" style="@style/tab_item_background" android:drawableTop="@drawable/tab_icon5" android:text="更多" /> </RadioGroup> </android.support.v4.app.FragmentTabHost> </LinearLayout>
public class MainActivity extends FragmentActivity{ private FragmentTabHost mTabHost; private RadioGroup m_radioGroup; String tabs[] = {"Tab1","Tab2","Tab3","Tab4","Tab5"}; //切換的fragment Class<?> cls[] = {Fragment1.class,Fragment2.class,Fragment3.class,Fragment4.class,Fragment5.class}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_fragment_tabs); init(); } private void init() { mTabHost = (FragmentTabHost)this.findViewById(android.R.id.tabhost); //初始化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); } //RadioGroup設置監聽改變狀態 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; case R.id.RadioButton2: mTabHost.setCurrentTabByTag(tabs[2]); break; case R.id.RadioButton3: mTabHost.setCurrentTabByTag(tabs[3]); break; case R.id.RadioButton4: mTabHost.setCurrentTabByTag(tabs[4]); break; } } }); ((RadioButton) m_radioGroup.getChildAt(0)).toggle(); } }
public class Fragment1 extends Fragment { //其中的具體內容可自行添加 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub return super.onCreateView(inflater, container, savedInstanceState); } }
主佈局文件:activity_main.xml app
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent" android:layout_height="fill_parent" tools:context=".MainActivity" android:orientation="vertical" > <!-- 設置內容的權重爲1 --> <FrameLayout android:id="@+id/fragment_content" android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1" /> <!-- 設置選項卡的權重爲默認 --> <android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="fill_parent" android:background="#E6E6FA" android:layout_height="wrap_content" > <!-- 設置每一個選項卡之間權重爲1 --> <FrameLayout android:id="@+id/fragment_tabmost_item" android:layout_width="wrap_content" android:layout_height="0dp" android:layout_weight="1" /> </android.support.v4.app.FragmentTabHost> </LinearLayout>
public class MainActivity extends FragmentActivity { FrameLayout content_frameLayout; Class<?>[] frames = {Fragment1.class,Fragment2.class,Fragment3.class,Fragment4.class}; private final String[] titles = {"資料","門診","住院","疾病"}; private final int[] images = {R.drawable.tab_information,R.drawable.tab_outpatient, R.drawable.tab_hospital,R.drawable.tab_disease}; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initWidget(); } private void initWidget() { // TODO Auto-generated method stub FragmentTabHost tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost); //初始化tabhost內容 tabHost.setup(this, getSupportFragmentManager(), R.id.fragment_content); //爲每一個tabhost設置內容 for(int i=0;i<titles.length;i++){ TabSpec tabSpec = tabHost.newTabSpec(titles[i]).setIndicator(getTabItemView(i)); tabHost.addTab(tabSpec,frames[i],null); tabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.main_tab_bg); } } /** * 給Tab按鈕設置圖標和文字 */ private View getTabItemView(int index){ View view = LayoutInflater.from(this).inflate(R.layout.tab_item_view, null); ImageView imageView = (ImageView) view.findViewById(R.id.imageview); imageView.setImageResource(images[index]); TextView textView = (TextView) view.findViewById(R.id.textview); textView.setText(titles[index]); return view; } }
比較以上的兩種方式,我的以爲第二種方式更簡便些,固然兩種方式都能知足咱們的要求。取捨須要本身去判斷了。。。 ide