1.布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#043"
>
<RadioGroup
android:id="@+id/mainscreen_radiogroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:gravity="center_vertical"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:background="@drawable/btn_normal"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/one"
style="@style/mainscreen_tab_btn"
android:background="@drawable/selector_tab_btn"
android:checked="true"
android:text="one" />
<RadioButton
android:id="@+id/two"
style="@style/mainscreen_tab_btn"
android:background="@drawable/selector_tab_btn"
android:text="two" />
<RadioButton
android:id="@+id/three"
style="@style/mainscreen_tab_btn"
android:background="@drawable/selector_tab_btn"
android:text="three" />
</RadioGroup>
<TabHost
android:id="@+id/tabs"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_marginTop="20dp"
android:layout_below="@id/mainscreen_radiogroup"
android:layout_weight="1.0" >
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:visibility="gone" />
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white" >
<LinearLayout
android:id="@+id/layout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="one" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="two" />
</LinearLayout>
<LinearLayout
android:id="@+id/layout3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="three" />
</LinearLayout>
</FrameLayout>
</TabHost>
</RelativeLayout>
package cn.bo;
import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TabHost;
public class DemoTabhostActivity extends Activity {
private TabHost tabHost;
public static RadioGroup radioGroup;
public static final String TAB_OPTIMIZATION = "taboptimization";
public static final String TAB_SAVE = "tabsave";
public static final String TAB_MONITOR = "tabmonitor";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.demotabhost);
tabHost=(TabHost)findViewById(R.id.tabs);
tabHost.setup();//必须调用实例化控件、
tabHost.addTab(tabHost.newTabSpec(TAB_OPTIMIZATION)
.setIndicator("tab1")
.setContent(R.id.layout1));
tabHost.addTab(tabHost.newTabSpec(TAB_SAVE)
.setIndicator("tab2")
.setContent(R.id.layout2));
tabHost.addTab(tabHost.newTabSpec(TAB_MONITOR)
.setIndicator("tab3")
.setContent(R.id.layout3));
radioGroup=(RadioGroup)this.findViewById(R.id.mainscreen_radiogroup);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.one:
tabHost.setCurrentTabByTag(TAB_OPTIMIZATION);
break;
case R.id.two:
tabHost.setCurrentTabByTag(TAB_SAVE);
break;
case R.id.three:
tabHost.setCurrentTabByTag(TAB_MONITOR);
break;
}
}
});
}
}
