公司项目,需求本来是按照谷歌官方指南写的,菜单栏设计成在导航栏下方
结果呢,审评时,BOSS为了和iOS统一,改成了底部菜单栏(标准结局),我只能呵呵呵呵呵呵呵
查了查资料发现实现底部菜单栏用的是FragmentTabHost,下面记录下具体如何实现的
首先,假设我有3个菜单栏,对应3个Fragment:FragmentA、FragmentB、FragmentC
这3个Fragment将由一个Activity控制:TabHostActivity
TabHostActivity对应的xml文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"> <FrameLayout
android:id="@+id/real_tab_content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/> <RadioGroup
android:id="@+id/radio_tab_bottom_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#111111"
android:orientation="horizontal"> <RadioButton
android:id="@+id/tab_patient_list"
style="@style/tab_rb_style"
android:checked="true"
android:text="@string/tab_patient_list"/> <RadioButton
android:id="@+id/tab_message"
style="@style/tab_rb_style"
android:text="@string/tab_message"/> <RadioButton
android:id="@+id/tab_settings"
style="@style/tab_rb_style"
android:text="@string/tab_settings"/> </RadioGroup> <android.support.v4.app.FragmentTabHost
android:id="@android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" > <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
</android.support.v4.app.FragmentTabHost> </LinearLayout>
其中,id为real_tab_content的fragment存放用于显示的Fragment。
TabHostActivity:
public class TabHostActivity extends FragmentActivity{
private FragmentTabHost mFragmentTabHost;
private RadioGroup mTabRg; private final Class[] fragments = {
FragmentA.class,
FragmentB.class,
FragmentC.class
}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bottom_menu); initView();
} private void initView() {
// 构建TabHost
mFragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
// getSupportFragmentManager():
// return the fragmentManager for interacting with fragments associated with this activity.
// setup(Context context, FragmentManager manager, int containerId)
mFragmentTabHost.setup(this, getSupportFragmentManager(), R.id.real_tab_content); int count = fragments.length;
for (int i = 0; i < count; i++) {
// A tab has a tab indicator, content, and a tag that is used to keep track of it.
// newTabSpec(String tag):
// Get a new TabHost.TabSpec associated with this tab host.
TabHost.TabSpec tabSpec = mFragmentTabHost.newTabSpec(i + "").setIndicator(i + "");
mFragmentTabHost.addTab(tabSpec, fragments[i], null);
} mTabRg = (RadioGroup) findViewById(R.id.radio_tab_bottom_menu); mTabRg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
switch (i) {
case R.id.tab_patient_list:
mFragmentTabHost.setCurrentTab(0);
break; case R.id.tab_message:
mFragmentTabHost.setCurrentTab(1);
break; case R.id.tab_settings:
mFragmentTabHost.setCurrentTab(2);
break; default:
break;
}
}
});
} }
FragmentA:
public class FragmentA extends Fragment {
private View rootView; @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (rootView == null) {
rootView = inflater.inflate(R.layout.fragment_settings, container, false);
}
ViewGroup parent = (ViewGroup) rootView.getParent();
if (parent != null) {
parent.removeView(rootView);
} return rootView;
}
}
FragmentB、C同理。