现在一般的app都使用底部菜单栏,那具体怎么实现的呢!我们就来看看

首先给大家展示一下布局文件

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 android:orientation="vertical" android:layout_width="match_parent"
3 android:layout_height="match_parent">
4 <FrameLayout
5 android:id="@+id/realtabcontent"
6 android:layout_width="fill_parent"
7 android:layout_height="0dip"
8 android:layout_weight="1"
9 android:background="@color/white" />
10
11
12 <LinearLayout
13 android:layout_width="match_parent"
14 android:layout_height="wrap_content"
15 android:layout_gravity="bottom"
16 android:orientation="vertical">
17
18 <View
19 android:layout_width="match_parent"
20 android:layout_height="1px"
21 android:background="@color/color_home_tab_line" />
22
23 <android.support.v4.app.FragmentTabHost
24 android:id="@android:id/tabhost"
25 android:layout_width="fill_parent"
26 android:layout_height="wrap_content"
27 android:background="@color/et_divider_disable">
28
29 <FrameLayout
30 android:id="@android:id/tabcontent"
31 android:layout_width="0dp"
32 android:layout_height="0dp"
33 android:layout_weight="0" />
34 </android.support.v4.app.FragmentTabHost>
35
36 </LinearLayout>
37 </LinearLayout>

接下来就是怎么使用了,其实比较简单,我们就看代码吧!

 //数据
private int mImageViewArray[] = {R.drawable.home_tab1, R.drawable.home_tab2, R.drawable.home_tab3};
private String mTextviewArray[] = {"首页", "设置","我的"};
private Class fragmentArray[] = {Fragment1.class, Fragment2.class, Fragment3.class}; //初始化以及设置数据
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
int count = fragmentArray.length;
for (int i = 0; i < count; i++) {
TabHost.TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
.setIndicator(getTabItemView(i));
mTabHost.addTab(tabSpec, fragmentArray[i], null);
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.bg_tbitem);
}
mTabHost.setCurrentTabByTag(mTextviewArray[0]);
mTabHost.getTabWidget().setDividerDrawable(null); mTabHost.setOnTabChangedListener(new TabHost.OnTabChangeListener() {
@Override
public void onTabChanged(String s) { }
}); /**
* 项的样式
*
* @param index 第几个
* @return 每一个Tab样式
*/
private View getTabItemView(int index) {
View view = layoutInflater.inflate(R.layout.tab_home_item, null);
ImageView imageView = (ImageView) view.findViewById(R.id.icon);
imageView.setImageResource(mImageViewArray[index]);
TextView textView = (TextView) view.findViewById(R.id.name);
textView.setText(mTextviewArray[index]);
return view;
}

对了,其中图片我们都设置成了selector,才有点击变色的效果

 <selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/btn_jiance_pre" android:state_pressed="false" android:state_selected="true" />
<item android:drawable="@drawable/btn_jiance_nor" android:state_focused="false" android:state_pressed="false" android:state_selected="false" />
<item android:drawable="@drawable/btn_jiance_pre" android:state_focused="true" android:state_pressed="true" />
</selector>

FragmentTabHost+FrameLayout实现底部菜单栏的更多相关文章

  1. 【Android UI设计与开发】5.底部菜单栏(二)使用Fragment实现底部菜单栏

    既然 Fragment 取代了TabActivity,当然 TabActivity 的能实现的菜单栏,Fragment 当然也能实现.主要其实就是通过菜单栏的点击事件切换 Fragment 的显示和隐 ...

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

    公司项目,需求本来是按照谷歌官方指南写的,菜单栏设计成在导航栏下方 结果呢,审评时,BOSS为了和iOS统一,改成了底部菜单栏(标准结局),我只能呵呵呵呵呵呵呵 查了查资料发现实现底部菜单栏用的是Fr ...

  3. FragmentTabHostBottomDemo【FragmentTabHost + Fragment实现底部选项卡】

    版权声明:本文为HaiyuKing原创文章,转载请注明出处! 前言 使用FragmentTabHost实现底部选项卡效果. 备注:该Demo主要是演示FragmentTabHost的一些设置和部分功能 ...

  4. 底部菜单栏(二) TabHost & RadioGroup 实现

    需求:使用TabHost & RadioGroup实现底部菜单栏: 效果图: 实现分析: 1.目录结构: 代码实现: 1. activity_main.xml <?xml version ...

  5. 底部菜单栏(一) TabHost实现

    需求:使用TabHost实现底部菜单栏: 效果图: 实现分析: 1.目录结构: 代码实现: 1.activity_main.xml <?xml version="1.0" e ...

  6. 我的Android之路——底部菜单栏的实现

    底部菜单栏的实现 底部菜单栏两种实现方法:ViewPager:可滑动的界面:Fragment:固定的界面. 首先,页面布局,在除去顶部toolbar之后,将主界面分为两部分,一部分为界面显示区,另一部 ...

  7. Android典型界面设计——FragmentTabHost+Fragment实现底部tab切换

    一.问题描述 在上次博文中,我们使用RadioGroup+ViewPage+Fragmen实现了顶部滑动导航(查看文章:http://www.cnblogs.com/jerehedu/p/460759 ...

  8. Android底部菜单栏+顶部菜单

    底部菜单栏+顶部菜单(wechat)demo http://blog.csdn.net/evankaka/article/details/44121457 底部菜单demo http://blog.c ...

  9. 底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏

    一.实现效果图 二.项目工程结构 三.详细代码编写 1.主tab布局界面,main_tab_layout: 双击代码全选 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

随机推荐

  1. Codeforces 704A Thor 队列模拟

    题目大意:托尔有一部手机可执行三种操作 1.x APP产生一个新消息 2.读取x App已产生的所有消息 3.读取前t个产生的消息 问每次操作后未读取的消息的数量 题目思路: 队列模拟,坑点在于竟然卡 ...

  2. KVC 实战浅析

    KVC 就是 key value coding,废话! 今天我们研究的是如何使用它! key value coding : 间接通过字符串类型的key取出对应的属性值 KVC的价值 1.可以访问私有成 ...

  3. HDU 1043 八数码(八境界)

    看了这篇博客的讲解,挺不错的.http://www.cnblogs.com/goodness/archive/2010/05/04/1727141.html 判断无解的情况(写完七种境界才发现有直接判 ...

  4. java项目(java project)如何导入jar包的解决方案列表

    右键项目-properties-java build path(左侧菜单)-选择libraries 有两种方式,导入jar包实际上就是建立一种链接,并不是copy式的导入 一.导入外部包,add ex ...

  5. 用cas来实现php的单点登陆

    最近项目中需要做单点登录,客户端包含Java.ruby和PHP,java有几个应用程序,php是discuz+supesite+ucenter,配置步骤如下: 1.cas服务端:下载地址:http:/ ...

  6. 《Peering Inside the PE: A Tour of the Win32 Portable Executable File Format》阅读笔记二

    Common Sections The .text section is where all general-purpose code emitted by the compiler or assem ...

  7. ping命令使用技巧(一次Ping多个地址)

    打开windows 命令行 窗口, 在命令行输入以下命令: for /l %i in (1,1,255) do ping -n 1  -w 60 192.168.0.%i  | find " ...

  8. Chapter 1 First Sight——17

    Once I got around the cafeteria, building three was easy to spot. 我一走出自助餐厅,很容易看见建筑三. A large black & ...

  9. SVM阅读资料

    1,Andrew Ng机器学习公开课笔记 -- 支持向量机 2,http://blog.pluskid.org/?page_id=683 3,支持向量机SVM(一) 4,机器学习中的算法(2)-支持向 ...

  10. ssh-copy-id

    建立无密码登录是经现root成功普通用户失败, chmod 0600 authorized_keys setenforce 0 ssh-copy-id  server2 ssh-add   ~/.ss ...