import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.View;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost.TabSpec;
public class MainActivity extends FragmentActivity implements
OnCheckedChangeListener {
FragmentTabHost tabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 找到FragmentTabHost
tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
// 设置TabHost R.id.realtabcontent 为显示Fragment的容器
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
// 创建Tabs,设置显示的标题
TabSpec tabSpecA = tabHost.newTabSpec("0").setIndicator("A");
TabSpec tabSpecB = tabHost.newTabSpec("1").setIndicator("B");
TabSpec tabSpecC = tabHost.newTabSpec("2").setIndicator("C");
TabSpec tabSpecD = tabHost.newTabSpec("3").setIndicator("D");
// 添加 tabs
tabHost.addTab(tabSpecA, FragmentA.class, null);
tabHost.addTab(tabSpecB, FragmentB.class, null);
tabHost.addTab(tabSpecC, FragmentC.class, null);
tabHost.addTab(tabSpecD, FragmentD.class, null);
tabHost.getTabWidget().setVisibility(View.GONE);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radiogroup1);
radioGroup.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radiobutton0:
tabHost.setCurrentTabByTag("0");
break;
case R.id.radiobutton1:
tabHost.setCurrentTabByTag("1");
break;
case R.id.radiobutton2:
tabHost.setCurrentTabByTag("2");
break;
case R.id.radiobutton3:
tabHost.setCurrentTabByTag("3");
break;
}
}
}
[quote]源码链接http://pan.baidu.com/share/link?shareid=2722935379&uk=2099615464[/quote]
布局文件
[quote]<LinearLayout 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"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" >
</LinearLayout>
<android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp" >
</FrameLayout>
</android.support.v4.app.FragmentTabHost>
<RadioGroup
android:id="@+id/radiogroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="@+id/radiobutton0"
style="@style/radiogroup"
android:text="消息"
android:drawableTop="@drawable/message"
/>
<RadioButton
android:id="@+id/radiobutton1"
style="@style/radiogroup"
android:drawableTop="@drawable/contact"
android:text="联系人"
/>
<RadioButton
android:id="@+id/radiobutton2"
style="@style/radiogroup"
android:textColor="@color/textcolor"
android:drawableTop="@drawable/news"
android:text="动态"
/>
<RadioButton
android:id="@+id/radiobutton3"
android:drawableTop="@drawable/setting"
android:text="设置"
style="@style/radiogroup"
/>
</RadioGroup>
</LinearLayout>[/quote]