首先布局xml中加入一个RelativeLayout来作为Fragment的容器,相当于Tabhost中的tabcontent;加入RadioGroup来作为选项卡,相当于Tabhost中的TabWidget。具体如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <!-- 相当于TabWidget --> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:orientation="horizontal" > <RadioButton android:id="@+id/radio0" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:checked="true" android:text="一" /> <RadioButton android:id="@+id/radio1" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="二" /> <RadioButton android:id="@+id/radio2" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="三" /> </RadioGroup> <!-- 相当于tabcontent --> <RelativeLayout android:id="@+id/content" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/radioGroup1" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" > </RelativeLayout> </RelativeLayout>
其次,在当前Activity中加入默认Fragment,并且通过RadioGroup的监听来进行切换Fragment。具体Activity代码如下:
package com.kenneth.blogtabfragment; import android.app.Activity; import android.app.Fragment; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.content.Context; import android.os.Bundle; import android.view.Menu; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; /** * @author Kenneth 2014-2-11 上午10:26:06 */ public class MainActivity extends Activity implements OnCheckedChangeListener { public static final String TAG = MainActivity.class.getSimpleName(); private Context mContext = this; private FragmentManager fm; private RadioGroup radioGroup1; private RadioButton radio0; private RadioButton radio1; private RadioButton radio2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initView(); initData(); initListener(); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.main, menu); return true; } private void initView() { radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1); radio0 = (RadioButton) findViewById(R.id.radio0); radio1 = (RadioButton) findViewById(R.id.radio1); radio2 = (RadioButton) findViewById(R.id.radio2); } private void initData() { fm = getFragmentManager();// 获取fragment管理工具类 replace(new FirstFragment());//默认选第一个 } private void initListener() { radioGroup1.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.radio0: replace(new FirstFragment()); break; case R.id.radio1: replace(new SecondFragment()); break; case R.id.radio2: replace(new ThirdFragment()); break; default: break; } } private void replace(Fragment fragment) {//替换当前容器中的Fragment FragmentTransaction beginTransaction = fm.beginTransaction(); beginTransaction.replace(R.id.content, fragment); beginTransaction.commit(); } }
以上,就是用Fragment来实现Tabhost效果的主要代码。如果是api11以下,请把getFragmentManager替换为getSupportFragmentManager,其他Fragment相关的请引v4支持包中的。
用多了你就会发现Fragment要比Tabhost,ActivityGroup好用太多了。