现在一般的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. 大并发大数量中的MYSQL瓶颈与NOSQL介绍

    NoSQL在2010年风生水起,大大小小的Web站点在追求高性能高可靠性方面,不由自主都选择了NoSQL技术作为优先考虑的方面.今年伊始,InfoQ中文站有幸邀请到凤凰网的孙立先生,为大家分享他之于N ...

  2. Swift POP+MVVM

    Swift2.0中引入了协议扩展的特性,并且建议开发者一切从协议(Protocol)出发,经过几个月的学习探索,博主发现Swift作为一门面向协议编程(POP)的语言非常适合时下火热的MVVM架构.M ...

  3. 在Activity之间使用Intent传值和Bundle传值的区别和方式

    两者本质上没有任何区别.Bundle只是一个信息的载体 将内部的内容以键值对组织 Intent负责Activity之间的交互 自己是带有一个Bundle的Intent.putExtras(Bundle ...

  4. UVALive 4031 Integer Transmission(贪心 + DP)

    分析:求出最大值和最小值比较简单,使用贪心法,求最小值的时候我们让所有的0尽可能的向后延迟就可以了,求最大值则相反. 关键在于求出可以组合出的数字个数. 这就是组合数学版的dp了,我们让dp[i][j ...

  5. 删除MySQL二进制日志

    服务器上的120G SSD硬盘空间用了92%,检查后发现,原来是 MySQL的二进制日志没有及时清除,占用了大量的空间, 于是直接用命令:reset master 一把删干净了. 1 reset ma ...

  6. 凹凸曼的修改zencart 程序(经典!)

    ==================================================================================================== ...

  7. logstash安装配置

    vim /usr/local/logstash/etc/hello_search.conf 输入下面: input { stdin { type => "human" }} ...

  8. 【转】PHP代码审计

    PHP代码审计 目录 1. 概述3 2. 输入验证和输出显示3 2.1 命令注入4 2.2 跨站脚本4 2.3 文件包含5 2.4 代码注入5 2.5 SQL注入6 2.6 XPath注入6 2.7 ...

  9. android的m、mm、mmm编译命令的使用

    android的m.mm.mmm编译命令的使用   android源码目录下的build/envsetup.sh文件,描述编译的命令 - m:       Makes from the top of ...

  10. map转换成list

    Java代码如下: package Test01; import java.util.ArrayList; import java.util.HashMap; import java.util.Ite ...