【Android开发笔记】底部菜单栏 FragmentTabHost

公司项目,需求本来是按照谷歌官方指南写的,菜单栏设计成在导航栏下方

结果呢,审评时,BOSS为了和iOS统一,改成了底部菜单栏(标准结局),我只能呵呵呵呵呵呵呵

查了查资料发现实现底部菜单栏用的是FragmentTabHost,下面记录下具体如何实现的

 

首先,假设我有3个菜单栏,对应3个Fragment:FragmentA、FragmentB、FragmentC

这3个Fragment将由一个Activity控制:TabHostActivity

 

TabHostActivity对应的xml文件:

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2               android:orientation="vertical"
 3               android:layout_width="match_parent"
 4               android:layout_height="match_parent">
 5 
 6     <FrameLayout
 7         android:id="@+id/real_tab_content"
 8         android:layout_width="match_parent"
 9         android:layout_height="0dp"
10         android:layout_weight="1"/>
11 
12     <RadioGroup
13         android:id="@+id/radio_tab_bottom_menu"
14         android:layout_width="match_parent"
15         android:layout_height="wrap_content"
16         android:background="#111111"
17         android:orientation="horizontal">
18 
19         <RadioButton
20             android:id="@+id/tab_patient_list"
21             style="@style/tab_rb_style"
22             android:checked="true"
23             android:text="@string/tab_patient_list"/>
24 
25         <RadioButton
26             android:id="@+id/tab_message"
27             style="@style/tab_rb_style"
28             android:text="@string/tab_message"/>
29 
30         <RadioButton
31             android:id="@+id/tab_settings"
32             style="@style/tab_rb_style"
33             android:text="@string/tab_settings"/>
34 
35     </RadioGroup>
36 
37     <android.support.v4.app.FragmentTabHost
38         android:id="@android:id/tabhost"
39         android:layout_width="match_parent"
40         android:layout_height="wrap_content"
41         android:visibility="gone" >
42 
43         <FrameLayout
44             android:id="@android:id/tabcontent"
45             android:layout_width="0dp"
46             android:layout_height="0dp"
47             android:layout_weight="0"/>
48     </android.support.v4.app.FragmentTabHost>
49 
50 </LinearLayout>

其中,id为real_tab_content的fragment存放用于显示的Fragment。

 

TabHostActivity:

 1 public class TabHostActivity extends FragmentActivity{
 2     private FragmentTabHost mFragmentTabHost;
 3     private RadioGroup mTabRg;
 4 
 5     private final Class[] fragments = {
 6             FragmentA.class,
 7             FragmentB.class,
 8             FragmentC.class
 9     };
10 
11     @Override
12     protected void onCreate(Bundle savedInstanceState) {
13         super.onCreate(savedInstanceState);
14         setContentView(R.layout.activity_bottom_menu);
15 
16         initView();
17     }
18 
19     private void initView() {
20         // 构建TabHost
21         mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
22         // getSupportFragmentManager():
23         // return the fragmentManager for interacting with fragments associated with this activity.
24         // setup(Context context, FragmentManager manager, int containerId)
25         mFragmentTabHost.setup(this, getSupportFragmentManager(), R.id.real_tab_content);
26 
27         int count = fragments.length;
28         for (int i = 0; i < count; i++) {
29             // A tab has a tab indicator, content, and a tag that is used to keep track of it.
30             // newTabSpec(String tag):
31             // Get a new TabHost.TabSpec associated with this tab host.
32             TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(i + "").setIndicator(i + "");
33             mFragmentTabHost.addTab(tabSpec, fragments[i], null);
34         }
35 
36         mTabRg = (RadioGroup) findViewById(R.id.radio_tab_bottom_menu);
37 
38         mTabRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
39             @Override
40             public void onCheckedChanged(RadioGroup radioGroup, int i) {
41                 switch (i) {
42                     case R.id.tab_patient_list:
43                         mFragmentTabHost.setCurrentTab(0);
44                         break;
45 
46                     case R.id.tab_message:
47                         mFragmentTabHost.setCurrentTab(1);
48                         break;
49 
50                     case R.id.tab_settings:
51                         mFragmentTabHost.setCurrentTab(2);
52                         break;
53 
54                     default:
55                         break;
56                 }
57             }
58         });
59     }
60 
61 }

FragmentA:

 1 public class FragmentA extends Fragment {
 2     private View rootView;
 3 
 4     @Override
 5     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
 6         if (rootView == null) {
 7             rootView = inflater.inflate(R.layout.fragment_settings, container, false);
 8         }
 9      
10         ViewGroup parent = (ViewGroup) rootView.getParent();
11         if (parent != null) {
12             parent.removeView(rootView);
13         }
14 
15         return rootView;
16     }
17 }

FragmentB、C同理。

 
posted @ 2015-07-07 11:54  死烤鹅  阅读(581)  评论(0编辑  收藏  举报