FragmentTabHost+RadioGroup(RadioButton)创建底部导航菜单(默认顶部_隐藏)
来源:互联网 发布:下载会声会影软件 编辑:程序博客网 时间:2024/06/04 17:40
步骤:
1)布局文件中声明RadioGroup和RadioButton
另外还有两个fragment_product和fragment_me以及它两个对应的java文件ProductFragment和MyFragment。就不写出代码了
1)布局文件中声明RadioGroup和RadioButton
2)设置RadioButton不同状态使用不同图片
3)设置RadioButton不同状态文字颜色的不同
4)修饰RadioButton5)描述RadioButton选中时切换界面(java)
activity_main中的代码:
<android.support.v4.app.FragmentTabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <!-- FragmentTabHost中的标签部分 --> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" > </TabWidget> <!-- FragmentTabHost中的内容部分 --> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:visibility="gone" > </FrameLayout> <!-- FragmentTabHost真正的标签内容部分 --> <FrameLayout android:id="@+id/main_realTabcontent" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" > </FrameLayout> </LinearLayout> </android.support.v4.app.FragmentTabHost><include layout="@layout/footer_menu"/>footer_menu中的代码:
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/footermenu_rgroup" android:layout_width="match_parent" android:layout_height="50dp" android:orientation="horizontal" android:background="@drawable/footer_menubg" android:paddingTop="4dp" android:paddingBottom="4dp" android:checkedButton="@+id/footmenu_btn_hone"><!-- 设置初始选中footmenu_btn_hone --> <RadioButton android:id="@id/footmenu_btn_hone" android:text="首页" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:button="@null" android:textSize="12sp" android:textColor="@color/footer_menu_textcolor" android:drawableTop="@drawable/footer_menu_home" /><RadioButton android:id="@+id/footmenu_btn_me" android:text="我" android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1" android:gravity="center" android:button="@null" android:textSize="12sp" android:textColor="@color/footer_menu_textcolor" android:drawableTop="@drawable/footer_menu_me"/></RadioGroup>
另外还有两个fragment_product和fragment_me以及它两个对应的java文件ProductFragment和MyFragment。就不写出代码了
public class MainActivity extends FragmentActivity {FragmentTabHost tabHost;RadioGroup radioGroup;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);initTabHost();initRadioGroup();}/** * 初始化TabHost */private void initTabHost() {// 必须掉用的语句tabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);// 第三个参数是layout文件中TabHost真正内容的FrameLayout的id值tabHost.setup(this, getSupportFragmentManager(),R.id.main_realTabcontent);
<span style="white-space:pre"></span>// 去掉tabHost显示的默认顶部的标签(和底部一致)<span style="white-space:pre"></span>findViewById(android.R.id.tabs).setVisibility(View.GONE) ; // 设置TabWidget部分不可见且不占用空间<span style="white-space:pre"></span><span style="white-space:pre"></span>// 给FragmentTabHost添加标签页<span style="white-space:pre"></span>tabHost.addTab(<span style="white-space:pre"></span>tabHost.newTabSpec("product")<span style="white-space:pre"></span>.setIndicator("商品")<span style="white-space:pre"></span>,<span style="white-space:pre"></span>// 该参数设置标签页的标识字符串以及显示的字符串<span style="white-space:pre"></span>ProductFragment.class , <span style="white-space:pre"></span>// 标签内容对应的Fragment对象<span style="white-space:pre"></span>null<span style="white-space:pre"></span>// Fragment初始化时如果需要传参,则该参数为Bundle对象;如果Fragment初始化时不需要参数,则此处使用null<span style="white-space:pre"></span><span style="white-space:pre"></span>);<span style="white-space:pre"></span>tabHost.addTab(<span style="white-space:pre"></span>tabHost.newTabSpec("me").setIndicator("我")<span style="white-space:pre"></span>,<span style="white-space:pre"></span>// 该参数设置标签页的标识字符串以及显示的字符串<span style="white-space:pre"></span>MeFragment.class , <span style="white-space:pre"></span>// 标签内容对应的Fragment对象<span style="white-space:pre"></span>null<span style="white-space:pre"></span>// Fragment初始化时如果需要传参,则该参数为Bundle对象;如果Fragment初始化时不需要参数,则此处使用null<span style="white-space:pre"></span><span style="white-space:pre"></span>);}private void initRadioGroup() {radioGroup = (RadioGroup) findViewById(R.id.footermenu_rgroup);radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {switch (checkedId) {case R.id.footmenu_btn_product:tabHost.setCurrentTab(0);break;case R.id.footmenu_btn_me:tabHost.setCurrentTab(1);break;}}});}}
0 0
- FragmentTabHost+RadioGroup(RadioButton)创建底部导航菜单(默认顶部_隐藏)
- FragmentTabHost实现底部导航菜单
- RadioButton实现底部菜单导航
- FragmentTabHost实现底部导航栏和顶部滑动导航
- Android (FragmentTabHost+RadioGroup)实现底部bar
- FragmentTabHost+RadioButton实现底部导航栏(带小红点提示)
- TabHost、RadioGroup实现底部菜单导航
- TabHost、RadioGroup、ToolBar实现底部菜单导航
- 底部按钮RadioButton、RadioGroup
- android 创建底部导航栏 (radiogroup+fragment)
- Android封装好的类似于RadioGroup的底部导航或顶部导航、仿微信底部导航
- 底部导航栏FragmentTabHost
- FragmentTabHost实现底部导航
- FragmentTabHost顶部菜单选项
- RadioGroup实现底部导航
- 底部导航之RadioGroup
- android实现底部导航栏和顶部导航栏(相当于网页上的一级菜单和二级菜单)
- Android 碎片底部(RadioButton)导航栏
- ios检测设备摄像头、指南针、录音、陀螺仪的状态
- 快速傅立叶变换的C语言实现方法
- 一个极度简单的写Excel文件实现
- quartz源码解析(一)
- eclipse中j2ee(struts2)部署及相关问题释疑
- FragmentTabHost+RadioGroup(RadioButton)创建底部导航菜单(默认顶部_隐藏)
- Android Studio快捷键的设置
- 《c++编程思想第2卷》第四章练习题4-15答案
- HDU 5090 Game with Pearls【贪心||模拟||二分匹配】
- sqlserver 数据库修复
- 扩展kmp算法
- mysql 自定义函数一直返回0
- IOS与javascript的交互
- POJ 2752 Seek the Name, Seek the Fame(KMP)