【Android开发笔记】底部菜单栏 FragmentTabHost
2024-09-02 09:44:19
公司项目,需求本来是按照谷歌官方指南写的,菜单栏设计成在导航栏下方
结果呢,审评时,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同理。
最新文章
- PHP开发笔记:二维数组根据某一项来进行排序
- BIOS开启虚拟化
- ssh 协议执行repo sync 报错:Permission denied (publickey)
- Linux下守护进程初探
- Netty开发UDP协议
- 在线富文本编辑器FckEditor配置(.Net Framework 3.5)
- ARM的启动和中断向量表
- [转载]解决zabbix在configure时候遇到的问题(Ubuntu)
- 牛 JQuery视频笔记
- 大数据量场景下storm自定义分组与Hbase预分区完美结合大幅度节省内存空间
- ubuntu上安装apache2+mysql+php5-fpm(PHP5 - FastCGI Process Manager)
- CSS后代选择器“空格”和“>;”的使用辨析
- Vue (二) --- Vue对象提供的属性功能
- 特殊字符url编码以后再解码后出现错误(&;not , &;cent, &;curren, &;pound)
- python split()函数的用法
- BBWebImage 设计思路
- [android] 优酷环形菜单-相对布局练习
- Unity Shader _Time 的单位
- Label Propagation Algorithm LPA 标签传播算法解析及matlab代码实现
- 固态盘经常性蓝屏处理方法(WIN7/8)