import android.content.Intent;
import android.os.Bundle;
import android.view.Window;
import android.widget.RadioGroup;
import android.widget.TabHost;
import android.widget.RadioGroup.OnCheckedChangeListener;
private RadioGroup mainTab;
private TabHost mTabHost;
private Intent mHomeIntent;
private Intent mNewsIntent;
private Intent mInfoIntent;
private Intent mSearchIntent;
private Intent mMoreIntent;
private final static String TAB_TAG_HOME="tab_tag_home";
private final static String TAB_TAG_NEWS="tab_tag_news";
private final static String TAB_TAG_INFO="tab_tag_info";
private final static String TAB_TAG_SEARCH="tab_tag_search";
private final static String TAB_TAG_MORE="tab_tag_more";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
mainTab=(RadioGroup)findViewById(R.id.main_tab);
mainTab.setOnCheckedChangeListener(this);
prepareIntent();
setupIntent();
}
private void prepareIntent() {
mHomeIntent=new Intent(this, HomeActivity.class);
mNewsIntent=new Intent(this, NewsActivity.class);
mInfoIntent=new Intent(this, MyInfoActivity.class);
mSearchIntent=new Intent(this,SearchActivity.class);
mMoreIntent=new Intent(this, MoreActivity.class);
}
private void setupIntent() {
this.mTabHost=getTabHost();
TabHost localTabHost=this.mTabHost;
localTabHost.addTab(buildTabSpec(TAB_TAG_HOME, R.string.main_home, R.drawable.icon_1_n, mHomeIntent));
localTabHost.addTab(buildTabSpec(TAB_TAG_NEWS, R.string.main_news, R.drawable.icon_2_n, mNewsIntent));
localTabHost.addTab(buildTabSpec(TAB_TAG_INFO, R.string.main_my_info, R.drawable.icon_3_n, mInfoIntent));
localTabHost.addTab(buildTabSpec(TAB_TAG_SEARCH, R.string.menu_search, R.drawable.icon_4_n, mSearchIntent));
localTabHost.addTab(buildTabSpec(TAB_TAG_MORE, R.string.more, R.drawable.icon_5_n, mMoreIntent));
}
private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,final Intent content) {
return this.mTabHost.newTabSpec(tag).
setIndicator(getString(resLabel),getResources().getDrawable(resIcon)).
setContent(content);
}
switch(checkedId){
case R.id.radio_button0:
this.mTabHost.setCurrentTabByTag(TAB_TAG_HOME);
break;
case R.id.radio_button1:
this.mTabHost.setCurrentTabByTag(TAB_TAG_NEWS);
break;
case R.id.radio_button2:
this.mTabHost.setCurrentTabByTag(TAB_TAG_INFO);
break;
case R.id.radio_button3:
this.mTabHost.setCurrentTabByTag(TAB_TAG_SEARCH);
break;
case R.id.radio_button4:
this.mTabHost.setCurrentTabByTag(TAB_TAG_MORE);
break;
}
}
}
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="0.0dip"
android:layout_weight="1.0"/>
<TabWidget
android:id="@android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.0"
android:visibility="gone"/>
<RadioGroup
android:id="@+id/main_tab"
android:background="@drawable/maintab_toolbar_bg"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_gravity="bottom">
<RadioButton
android:layout_marginTop="2.0dip"
android:text="@string/main_home"
android:drawableTop="@drawable/icon_1_n"
android:id="@+id/radio_button0"
style="@style/main_tab_bottom"/>
<RadioButton
android:layout_marginTop="2.0dip"
android:text="@string/main_news"
android:drawableTop="@drawable/icon_2_n"
android:id="@+id/radio_button1"
style="@style/main_tab_bottom"/>
<RadioButton
android:layout_marginTop="2.0dip"
android:text="@string/main_my_info"
android:drawableTop="@drawable/icon_3_n"
android:id="@+id/radio_button2"
style="@style/main_tab_bottom"/>
<RadioButton
android:layout_marginTop="2.0dip"
android:text="@string/menu_search"
android:drawableTop="@drawable/icon_4_n"
android:id="@+id/radio_button3"
style="@style/main_tab_bottom"/>
<RadioButton
android:layout_marginTop="2.0dip"
android:text="@string/more"
android:drawableTop="@drawable/icon_5_n"
android:id="@+id/radio_button4"
style="@style/main_tab_bottom"/>
</RadioGroup>
</LinearLayout>
</TabHost>
<resources>
<style name="main_tab_bottom">
<item name="android:textSize">@dimen/bottom_tab_font_size</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:ellipsize">marquee</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:background">@drawable/home_btn_bg</item>
<item name="android:paddingTop">@dimen/bottom_tab_padding_up</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:button">@null</item>
<item name="android:singleLine">true</item>
<item name="android:drawablePadding">@dimen/bottom_tab_padding_drawable</item>
<item name="android:layout_weight">1.0</item>
</style>
</resources>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_enabled="true"
android:state_pressed="false"
android:drawable="@drawable/home_btn_bg_s" />
<item android:state_enabled="true"
android:state_pressed="true"
android:drawable="@drawable/home_btn_bg_s" />
<item android:state_enabled="true"
android:state_checked="true"
android:drawable="@drawable/home_btn_bg_d" />
<item android:drawable="@drawable/transparent" />
</selector>