Android底部导航栏,三种风格和实现

一、效果图展示

%title插图%num

如果动图没有动的话,也可以看下面这个静态图

%title插图%num

以下挨个分析每个的实现,这里只做简单的效果展示,大家可以基于目前代码做二次开发。

二、BottomNavigationView
这是 Google 给我们提供的一个专门用于底部导航的 View,你只需要在新建 Activity 的时候选择 “Bottom Navigation Activity”,IDE 就会自动使用 BottomNavigationView 帮你生成好相应的代码了。

1. 在 xml 中使用
<android.support.design.widget.BottomNavigationView
android:id=”@+id/navigation”
android:layout_width=”0dp”
android:layout_height=”wrap_content”
android:layout_marginEnd=”0dp”
android:layout_marginStart=”0dp”
android:background=”?android:attr/windowBackground”
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:menu=”@menu/navigation” />
这里面唯一要注意的就是 app:menu 属性了,它指定了你的导航栏显示的页面菜单是怎样的。

2. menu 的布局文件
<?xml version=”1.0″ encoding=”utf-8″?>
<menu xmlns:android=”http://schemas.android.com/apk/res/android”>

<item
android:id=”@+id/navigation_home”
android:icon=”@drawable/ic_home_black_24dp”
android:title=”@string/title_home” />

<item
android:id=”@+id/navigation_dashboard”
android:icon=”@drawable/ic_dashboard_black_24dp”
android:title=”@string/title_dashboard” />

<item
android:id=”@+id/navigation_notifications”
android:icon=”@drawable/ic_notifications_black_24dp”
android:title=”@string/title_notifications” />

</menu>
3. 在 Activity 中调用
private TextView mTextMessage;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_style1);

mTextMessage = findViewById(R.id.message);
BottomNavigationView navigation = findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
这里的演示 code 都是 IDE 自动生成的,由于 BottomNavigationView 目前我还没有在项目中实际使用过,这里不做过多分析,使用起来不难,以上代码已经足以满足我们的基本使用要求了。

三、RadioGroup + ViewPager
这是一种比较常见了的,下面 4 个 tab 的导航按钮,可以切换不同的页面,这里页面使用了 ViewPager + Fragment 的组合,实现了滑动的页面效果,也可以不使用 ViewPager,这个根据产品的定义来使用即可。

1. 布局文件
<?xml version=”1.0″ encoding=”utf-8″?>
<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=”.style2.Style2Activity”>

<android.support.v4.view.ViewPager
android:id=”@+id/fragment_vp”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_above=”@+id/tabs_rg” />

<RadioGroup
android:id=”@+id/tabs_rg”
android:layout_width=”match_parent”
android:layout_height=”56dp”
android:layout_alignParentBottom=”true”
android:background=”#dcdcdc”
android:orientation=”horizontal”>

<RadioButton
android:id=”@+id/today_tab”
style=”@style/Custom.TabRadioButton”
android:checked=”true”
android:drawableTop=”@drawable/tab_sign_selector”
android:text=”今日” />

<RadioButton
android:id=”@+id/record_tab”
style=”@style/Custom.TabRadioButton”
android:drawableTop=”@drawable/tab_record_selector”
android:text=”记录” />

<RadioButton
android:id=”@+id/contact_tab”
style=”@style/Custom.TabRadioButton”
android:drawableTop=”@drawable/tab_contact_selector”
android:text=”通讯录” />

<RadioButton
android:id=”@+id/settings_tab”
style=”@style/Custom.TabRadioButton”
android:drawableTop=”@drawable/tab_setting_selector”
android:text=”设置” />
</RadioGroup>
</RelativeLayout>
2. Activity 类
public class Style2Activity extends AppCompatActivity {

private ViewPager mViewPager;
private RadioGroup mTabRadioGroup;

private List<Fragment> mFragments;
private FragmentPagerAdapter mAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_style2);
initView();
}

private void initView() {
// find view
mViewPager = findViewById(R.id.fragment_vp);
mTabRadioGroup = findViewById(R.id.tabs_rg);
// init fragment
mFragments = new ArrayList<>(4);
mFragments.add(BlankFragment.newInstance(“今日”));
mFragments.add(BlankFragment.newInstance(“记录”));
mFragments.add(BlankFragment.newInstance(“通讯录”));
mFragments.add(BlankFragment.newInstance(“设置”));
// init view pager
mAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager(), mFragments);
mViewPager.setAdapter(mAdapter);
// register listener
mViewPager.addOnPageChangeListener(mPageChangeListener);
mTabRadioGroup.setOnCheckedChangeListener(mOnCheckedChangeListener);
}

@Override
protected void onDestroy() {
super.onDestroy();
mViewPager.removeOnPageChangeListener(mPageChangeListener);
}

private ViewPager.OnPageChangeListener mPageChangeListener = new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

}

@Override
public void onPageSelected(int position) {
RadioButton radioButton = (RadioButton) mTabRadioGroup.getChildAt(position);
radioButton.setChecked(true);
}

@Override
public void onPageScrollStateChanged(int state) {

}
};

private RadioGroup.OnCheckedChangeListener mOnCheckedChangeListener = new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
for (int i = 0; i < group.getChildCount(); i++) {
if (group.getChildAt(i).getId() == checkedId) {
mViewPager.setCurrentItem(i);
return;
}
}
}
};

private class MyFragmentPagerAdapter extends FragmentPagerAdapter {

private List<Fragment> mList;

public MyFragmentPagerAdapter(FragmentManager fm, List<Fragment> list) {
super(fm);
this.mList = list;
}

@Override
public Fragment getItem(int position) {
return this.mList == null ? null : this.mList.get(position);
}

@Override
public int getCount() {
return this.mList == null ? 0 : this.mList.size();
}
}

}
这里唯一注意点的就是两个监听事件,要实现底部导航按钮和页面的联动。

四、带页面跳转功能的底部导航
很多 APP 的底部导航栏中间有一个很大的按钮,点击后通常是打开一个新的页面,这里我们要实现的就是这种底部导航。
依旧是使用 RadioGroup 来做,只不过中间一个 tab 我们先用一个空的 View 来占位,然后在这个 View 的位置放置一个较大的按钮来覆盖住。

1. 布局文件
<?xml version=”1.0″ encoding=”utf-8″?>
<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=”.style3.Style3Activity”>

<FrameLayout
android:id=”@+id/fragment_container”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:layout_above=”@+id/tabs_rg” />

<RadioGroup
android:id=”@+id/tabs_rg”
android:layout_width=”match_parent”
android:layout_height=”56dp”
android:layout_alignParentBottom=”true”
android:background=”#dcdcdc”
android:orientation=”horizontal”>

<RadioButton
android:id=”@+id/today_tab”
style=”@style/Custom.TabRadioButton”
android:checked=”true”
android:drawableTop=”@drawable/tab_sign_selector”
android:text=”今日” />

<RadioButton
android:id=”@+id/record_tab”
style=”@style/Custom.TabRadioButton”
android:drawableTop=”@drawable/tab_record_selector”
android:text=”记录” />

<View style=”@style/Custom.TabRadioButton” />

<RadioButton
android:id=”@+id/contact_tab”
style=”@style/Custom.TabRadioButton”
android:drawableTop=”@drawable/tab_contact_selector”
android:text=”通讯录” />

<RadioButton
android:id=”@+id/settings_tab”
style=”@style/Custom.TabRadioButton”
android:drawableTop=”@drawable/tab_setting_selector”
android:text=”设置” />
</RadioGroup>

<ImageView
android:id=”@+id/sign_iv”
android:layout_width=”80dp”
android:layout_height=”80dp”
android:layout_alignParentBottom=”true”
android:layout_centerHorizontal=”true”
android:background=”@android:color/transparent”
android:src=”@mipmap/sign” />
</RelativeLayout>
2. Activity 类
public class Style3Activity extends AppCompatActivity {

private RadioGroup mTabRadioGroup;
private SparseArray<Fragment> mFragmentSparseArray;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_style3);
initView();
}

private void initView() {
mTabRadioGroup = findViewById(R.id.tabs_rg);
mFragmentSparseArray = new SparseArray<>();
mFragmentSparseArray.append(R.id.today_tab, BlankFragment.newInstance(“今日”));
mFragmentSparseArray.append(R.id.record_tab, BlankFragment.newInstance(“记录”));
mFragmentSparseArray.append(R.id.contact_tab, BlankFragment.newInstance(“通讯录”));
mFragmentSparseArray.append(R.id.settings_tab, BlankFragment.newInstance(“设置”));
mTabRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// 具体的fragment切换逻辑可以根据应用调整,例如使用show()/hide()
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,
mFragmentSparseArray.get(checkedId)).commit();
}
});
// 默认显示*个
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container,
mFragmentSparseArray.get(R.id.today_tab)).commit();
findViewById(R.id.sign_iv).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(Style3Activity.this, SignActivity.class));
}
});
}

}
注意:

如果这里你也想使用 ViewPager 来展示 Fragment 的话,一定要注意这里的 RadioGroup 中间有一个占位的 View,即两者的监听事件里,实现联动时要考虑多个这个 View 的存在。

TabHost与RadioGroup结合完成的菜单

效果图:
%title插图%num

首先看布局文件:

Java代码
  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <TabHost android:id=“@android:id/tabhost” android:layout_width=“fill_parent” android:layout_height=“fill_parent”
  3.   xmlns:android=“http://schemas.android.com/apk/res/android”>
  4.     <LinearLayout
  5.         android:orientation=“vertical”
  6.         android:layout_width=“fill_parent”
  7.         android:layout_height=“fill_parent”>
  8.         <FrameLayout
  9.             android:id=“@android:id/tabcontent”
  10.             android:layout_width=“fill_parent”
  11.             android:layout_height=“0.0dip”
  12.             android:layout_weight=“1.0” />
  13.         <TabWidget
  14.             android:id=“@android:id/tabs”
  15.             android:visibility=“gone”
  16.             android:layout_width=“fill_parent”
  17.             android:layout_height=“wrap_content”
  18.             android:layout_weight=“0.0” />
  19.         <RadioGroup
  20.             android:gravity=“center_vertical”
  21.             android:layout_gravity=“bottom”
  22.             android:orientation=“horizontal”
  23.             android:id=“@+id/main_radio”
  24.             android:background=“@drawable/maintab_toolbar_bg”
  25.             android:layout_width=“fill_parent”
  26.             android:layout_height=“wrap_content”>
  27.             <RadioButton
  28.                 android:id=“@+id/radio_button0”
  29.                 android:tag=“radio_button0”
  30.                 android:layout_marginTop=“2.0dip”
  31.                 android:text=“@string/alarm”
  32.                 android:drawableTop=“@drawable/icon_1”
  33.                 style=“@style/main_tab_bottom” />
  34.             <RadioButton
  35.                 android:id=“@+id/radio_button1”
  36.                 android:tag=“radio_button1”
  37.                 android:layout_marginTop=“2.0dip”
  38.                 android:text=“@string/message”
  39.                 android:drawableTop=“@drawable/icon_2”
  40.                 style=“@style/main_tab_bottom” />
  41.             <RadioButton
  42.                 android:id=“@+id/radio_button2”
  43.                 android:tag=“radio_button2”
  44.                 android:layout_marginTop=“2.0dip”
  45.                 android:text=“@string/photo”
  46.                 android:drawableTop=“@drawable/icon_3”
  47.                 style=“@style/main_tab_bottom” />
  48.             <RadioButton
  49.                 android:id=“@+id/radio_button3”
  50.                 android:tag=“radio_button3”
  51.                 android:layout_marginTop=“2.0dip”
  52.                 android:text=“@string/music”
  53.                 android:drawableTop=“@drawable/icon_4”
  54.                 style=“@style/main_tab_bottom” />
  55.             <RadioButton
  56.                 android:id=“@+id/radio_button4”
  57.                 android:tag=“radio_button4”
  58.                 android:layout_marginTop=“2.0dip”
  59.                 android:text=“@string/setting”
  60.                 android:drawableTop=“@drawable/icon_5”
  61.                 style=“@style/main_tab_bottom” />
  62.         </RadioGroup>
  63.     </LinearLayout>
  64. </TabHost>
[java]  view plain copy

  1. <?xml version=“1.0” encoding=“UTF-8”?>
  2. <TabHost android:id=“@android:id/tabhost” android:layout_width=“fill_parent” android:layout_height=“fill_parent”
  3.   xmlns:android=“http://schemas.android.com/apk/res/android”>
  4.     <LinearLayout
  5.         android:orientation=“vertical”
  6.         android:layout_width=“fill_parent”
  7.         android:layout_height=“fill_parent”>
  8.         <FrameLayout
  9.             android:id=“@android:id/tabcontent”
  10.             android:layout_width=“fill_parent”
  11.             android:layout_height=“0.0dip”
  12.             android:layout_weight=“1.0” />
  13.         <TabWidget
  14.             android:id=“@android:id/tabs”
  15.             android:visibility=“gone”
  16.             android:layout_width=“fill_parent”
  17.             android:layout_height=“wrap_content”
  18.             android:layout_weight=“0.0” />
  19.         <RadioGroup
  20.             android:gravity=“center_vertical”
  21.             android:layout_gravity=“bottom”
  22.             android:orientation=“horizontal”
  23.             android:id=“@+id/main_radio”
  24.             android:background=“@drawable/maintab_toolbar_bg”
  25.             android:layout_width=“fill_parent”
  26.             android:layout_height=“wrap_content”>
  27.             <RadioButton
  28.                 android:id=“@+id/radio_button0”
  29.                 android:tag=“radio_button0”
  30.                 android:layout_marginTop=“2.0dip”
  31.                 android:text=“@string/alarm”
  32.                 android:drawableTop=“@drawable/icon_1”
  33.                 style=“@style/main_tab_bottom” />
  34.             <RadioButton
  35.                 android:id=“@+id/radio_button1”
  36.                 android:tag=“radio_button1”
  37.                 android:layout_marginTop=“2.0dip”
  38.                 android:text=“@string/message”
  39.                 android:drawableTop=“@drawable/icon_2”
  40.                 style=“@style/main_tab_bottom” />
  41.             <RadioButton
  42.                 android:id=“@+id/radio_button2”
  43.                 android:tag=“radio_button2”
  44.                 android:layout_marginTop=“2.0dip”
  45.                 android:text=“@string/photo”
  46.                 android:drawableTop=“@drawable/icon_3”
  47.                 style=“@style/main_tab_bottom” />
  48.             <RadioButton
  49.                 android:id=“@+id/radio_button3”
  50.                 android:tag=“radio_button3”
  51.                 android:layout_marginTop=“2.0dip”
  52.                 android:text=“@string/music”
  53.                 android:drawableTop=“@drawable/icon_4”
  54.                 style=“@style/main_tab_bottom” />
  55.             <RadioButton
  56.                 android:id=“@+id/radio_button4”
  57.                 android:tag=“radio_button4”
  58.                 android:layout_marginTop=“2.0dip”
  59.                 android:text=“@string/setting”
  60.                 android:drawableTop=“@drawable/icon_5”
  61.                 style=“@style/main_tab_bottom” />
  62.         </RadioGroup>
  63.     </LinearLayout>
  64. </TabHost>

需要注意的是,如果用TabHost这个控件,其中有几个ID是必须这么写的,android:id=”@android:id/tabhost   ;android:id=”@android:id/tabcontent” ;android:id=”@android:id/tabs” ;之所以要这么写是因为在TabHost这个类中。需要实例化上述这个ID的控件。看源码:

在TabActivity中有么个方法:

  1. @Override
  2. public void onContentChanged() {
  3.     super.onContentChanged();
  4.     mTabHost = (TabHost) findViewById(com.android.internal.R.id.tabhost);
  5.     if (mTabHost == null) {
  6.         throw new RuntimeException(
  7.                 “Your content must have a TabHost whose id attribute is “ +
  8.                 “‘android.R.id.tabhost'”);
  9.     }
  10.     mTabHost.setup(getLocalActivityManager());
  11. }
  12. private void ensureTabHost() {
  13.     if (mTabHost == null) {
  14.         this.setContentView(com.android.internal.R.layout.tab_content);
  15.     }
  16. }
[java]  view plain copy

  1. @Override
  2. public void onContentChanged() {
  3.     super.onContentChanged();
  4.     mTabHost = (TabHost) findViewById(com.android.internal.R.id.tabhost);
  5.     if (mTabHost == null) {
  6.         throw new RuntimeException(
  7.                 “Your content must have a TabHost whose id attribute is “ +
  8.                 “‘android.R.id.tabhost'”);
  9.     }
  10.     mTabHost.setup(getLocalActivityManager());
  11. }
  12. private void ensureTabHost() {
  13.     if (mTabHost == null) {
  14.         this.setContentView(com.android.internal.R.layout.tab_content);
  15.     }
  16. }

当内容发生改变时它会调用这个方法,来更新列表或者其他视图,而这个方法中需要实例化TabHost,所以必须通过ID为tabhost实例化。

再看看TabHost这个类中,

Java代码
  1. public void setup() {
  2.     mTabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
  3.     if (mTabWidget == null) {
  4.         throw new RuntimeException(
  5.                 “Your TabHost must have a TabWidget whose id attribute is ‘android.R.id.tabs'”);
  6.     }
  7.     // KeyListener to attach to all tabs. Detects non-navigation keys 
  8.     // and relays them to the tab content. 
  9.     mTabKeyListener = new OnKeyListener() {
  10.         public boolean onKey(View v, int keyCode, KeyEvent event) {
  11.             switch (keyCode) {
  12.                 case KeyEvent.KEYCODE_DPAD_CENTER:
  13.                 case KeyEvent.KEYCODE_DPAD_LEFT:
  14.                 case KeyEvent.KEYCODE_DPAD_RIGHT:
  15.                 case KeyEvent.KEYCODE_DPAD_UP:
  16.                 case KeyEvent.KEYCODE_DPAD_DOWN:
  17.                 case KeyEvent.KEYCODE_ENTER:
  18.                     return false;
  19.             }
  20.             mTabContent.requestFocus(View.FOCUS_FORWARD);
  21.             return mTabContent.dispatchKeyEvent(event);
  22.         }
  23.     };
  24.     mTabWidget.setTabSelectionListener(new TabWidget.OnTabSelectionChanged() {
  25.         public void onTabSelectionChanged(int tabIndex, boolean clicked) {
  26.             setCurrentTab(tabIndex);
  27.             if (clicked) {
  28.                 mTabContent.requestFocus(View.FOCUS_FORWARD);
  29.             }
  30.         }
  31.     });
  32.     mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);
  33.     if (mTabContent == null) {
  34.         throw new RuntimeException(
  35.                 “Your TabHost must have a FrameLayout whose id attribute is “
  36.                         + “‘android.R.id.tabcontent'”);
  37.     }
  38. }
[java]  view plain copy

  1. public void setup() {
  2.     mTabWidget = (TabWidget) findViewById(com.android.internal.R.id.tabs);
  3.     if (mTabWidget == null) {
  4.         throw new RuntimeException(
  5.                 “Your TabHost must have a TabWidget whose id attribute is ‘android.R.id.tabs'”);
  6.     }
  7.     // KeyListener to attach to all tabs. Detects non-navigation keys
  8.     // and relays them to the tab content.
  9.     mTabKeyListener = new OnKeyListener() {
  10.         public boolean onKey(View v, int keyCode, KeyEvent event) {
  11.             switch (keyCode) {
  12.                 case KeyEvent.KEYCODE_DPAD_CENTER:
  13.                 case KeyEvent.KEYCODE_DPAD_LEFT:
  14.                 case KeyEvent.KEYCODE_DPAD_RIGHT:
  15.                 case KeyEvent.KEYCODE_DPAD_UP:
  16.                 case KeyEvent.KEYCODE_DPAD_DOWN:
  17.                 case KeyEvent.KEYCODE_ENTER:
  18.                     return false;
  19.             }
  20.             mTabContent.requestFocus(View.FOCUS_FORWARD);
  21.             return mTabContent.dispatchKeyEvent(event);
  22.         }
  23.     };
  24.     mTabWidget.setTabSelectionListener(new TabWidget.OnTabSelectionChanged() {
  25.         public void onTabSelectionChanged(int tabIndex, boolean clicked) {
  26.             setCurrentTab(tabIndex);
  27.             if (clicked) {
  28.                 mTabContent.requestFocus(View.FOCUS_FORWARD);
  29.             }
  30.         }
  31.     });
  32.     mTabContent = (FrameLayout) findViewById(com.android.internal.R.id.tabcontent);
  33.     if (mTabContent == null) {
  34.         throw new RuntimeException(
  35.                 “Your TabHost must have a FrameLayout whose id attribute is “
  36.                         + “‘android.R.id.tabcontent'”);
  37.     }
  38. }

这个方法,是在增加选项卡之前由系统调用。在这个方法中需要通过tabs 这个ID实例化一个TabWidget,通过tabcontent这个ID实例化一个FrameLayout,用来放置选项卡内容。所以这两个ID也是固定的。

在上述布局文件中隐藏了系统默认的Widget,取而代之的是带有图片的Button。

看一下主要代码:

Java代码
  1. package com.iteye.androidtoast;
  2. import android.app.TabActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.widget.RadioGroup;
  6. import android.widget.RadioGroup.OnCheckedChangeListener;
  7. import android.widget.TabHost;
  8. public class MainActivity extends TabActivity implements OnCheckedChangeListener{
  9.     /** Called when the activity is first created. */
  10.     private TabHost mHost;
  11.     private RadioGroup radioderGroup;
  12.     @Override
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.maintabs);
  16.         //实例化TabHost 
  17.         mHost=this.getTabHost();
  18.         //添加选项卡 
  19.         mHost.addTab(mHost.newTabSpec(“ONE”).setIndicator(“ONE”)
  20.                     .setContent(new Intent(this,OneActivity.class)));
  21.         mHost.addTab(mHost.newTabSpec(“TWO”).setIndicator(“TWO”)
  22.                 .setContent(new Intent(this,TwoActivity.class)));
  23.         mHost.addTab(mHost.newTabSpec(“THREE”).setIndicator(“THREE”)
  24.                 .setContent(new Intent(this,ThreeActivity.class)));
  25.         mHost.addTab(mHost.newTabSpec(“FOUR”).setIndicator(“FOUR”)
  26.                 .setContent(new Intent(this,FourActivity.class)));
  27.         mHost.addTab(mHost.newTabSpec(“FIVE”).setIndicator(“FIVE”)
  28.                 .setContent(new Intent(this,FiveActivity.class)));
  29.         radioderGroup = (RadioGroup) findViewById(R.id.main_radio);
  30.         radioderGroup.setOnCheckedChangeListener(this);
  31.     }
  32.     @Override
  33.     public void onCheckedChanged(RadioGroup group, int checkedId) {
  34.         switch(checkedId){
  35.         case R.id.radio_button0:
  36.             mHost.setCurrentTabByTag(“ONE”);
  37.             break;
  38.         case R.id.radio_button1:
  39.             mHost.setCurrentTabByTag(“TWO”);
  40.             break;
  41.         case R.id.radio_button2:
  42.             mHost.setCurrentTabByTag(“THREE”);
  43.             break;
  44.         case R.id.radio_button3:
  45.             mHost.setCurrentTabByTag(“FOUR”);
  46.             break;
  47.         case R.id.radio_button4:
  48.             mHost.setCurrentTabByTag(“FIVE”);
  49.             break;
  50.         }
  51.     }
  52. }
[java]  view plain copy

  1. package com.iteye.androidtoast;
  2. import android.app.TabActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.widget.RadioGroup;
  6. import android.widget.RadioGroup.OnCheckedChangeListener;
  7. import android.widget.TabHost;
  8. public class MainActivity extends TabActivity implements OnCheckedChangeListener{
  9.     /** Called when the activity is first created. */
  10.     private TabHost mHost;
  11.     private RadioGroup radioderGroup;
  12.     @Override
  13.     public void onCreate(Bundle savedInstanceState) {
  14.         super.onCreate(savedInstanceState);
  15.         setContentView(R.layout.maintabs);
  16.         //实例化TabHost
  17.         mHost=this.getTabHost();
  18.         //添加选项卡
  19.         mHost.addTab(mHost.newTabSpec(“ONE”).setIndicator(“ONE”)
  20.                     .setContent(new Intent(this,OneActivity.class)));
  21.         mHost.addTab(mHost.newTabSpec(“TWO”).setIndicator(“TWO”)
  22.                 .setContent(new Intent(this,TwoActivity.class)));
  23.         mHost.addTab(mHost.newTabSpec(“THREE”).setIndicator(“THREE”)
  24.                 .setContent(new Intent(this,ThreeActivity.class)));
  25.         mHost.addTab(mHost.newTabSpec(“FOUR”).setIndicator(“FOUR”)
  26.                 .setContent(new Intent(this,FourActivity.class)));
  27.         mHost.addTab(mHost.newTabSpec(“FIVE”).setIndicator(“FIVE”)
  28.                 .setContent(new Intent(this,FiveActivity.class)));
  29.         radioderGroup = (RadioGroup) findViewById(R.id.main_radio);
  30.         radioderGroup.setOnCheckedChangeListener(this);
  31.     }
  32.     @Override
  33.     public void onCheckedChanged(RadioGroup group, int checkedId) {
  34.         switch(checkedId){
  35.         case R.id.radio_button0:
  36.             mHost.setCurrentTabByTag(“ONE”);
  37.             break;
  38.         case R.id.radio_button1:
  39.             mHost.setCurrentTabByTag(“TWO”);
  40.             break;
  41.         case R.id.radio_button2:
  42.             mHost.setCurrentTabByTag(“THREE”);
  43.             break;
  44.         case R.id.radio_button3:
  45.             mHost.setCurrentTabByTag(“FOUR”);
  46.             break;
  47.         case R.id.radio_button4:
  48.             mHost.setCurrentTabByTag(“FIVE”);
  49.             break;
  50.         }
  51.     }
  52. }