记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果,但作为学习,还是需要来了解下这个新引入类FragmentTabHost

之前2篇文章的链接:

安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)

安卓开发复习笔记——TabHost组件(二)(实现底部菜单导航)

关于Fragment类在之前的安卓开发复习笔记——Fragment+ViewPager组件(高仿微信界面)也介绍过,这里就不再重复阐述了。

国际惯例,先来张效果图:

下面直接上代码了,注释很全,看过我前2篇文章的朋友,肯定秒懂的,哈哈~

activity_main.xml(主布局文件)

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6.  
  7. <!-- 存放主要页面内容 -->
  8.  
  9. <FrameLayout
  10. android:id="@+id/maincontent"
  11. android:layout_width="fill_parent"
  12. android:layout_height="0dp"
  13. android:layout_weight="1" >
  14. </FrameLayout>
  15.  
  16. <!-- 底层菜单 -->
  17.  
  18. <android.support.v4.app.FragmentTabHost
  19. android:id="@android:id/tabhost"
  20. android:layout_width="fill_parent"
  21. android:layout_height="wrap_content"
  22. android:background="@drawable/maintab_toolbar_bg" >
  23.  
  24. <FrameLayout
  25. android:id="@android:id/tabcontent"
  26. android:layout_width="0dp"
  27. android:layout_height="0dp"
  28. android:layout_weight="0" >
  29. </FrameLayout>
  30. </android.support.v4.app.FragmentTabHost>
  31.  
  32. </LinearLayout>

fragment.xml(由于只有文字不同,这里只给出一个)

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. >
  6.  
  7. <TextView
  8. android:id="@+id/text"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:layout_centerInParent="true"
  12. android:text="我是第一个Fragment"
  13. android:textSize="20dp"
  14. />
  15.  
  16. </RelativeLayout>

tabcontent.xml(具体底部菜单详细布局)

  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:gravity="center_horizontal"
  6. android:orientation="vertical" >
  7.  
  8. <ImageView
  9. android:id="@+id/image"
  10. android:layout_height="wrap_content"
  11. android:layout_width="wrap_content"
  12. />
  13. <TextView
  14. android:id="@+id/text"
  15. android:padding="2dp"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:textColor="@android:color/white"
  19. />
  20.  
  21. </LinearLayout>

bt_selector.xml(底部菜单点击背景)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3.  
  4. <item android:drawable="@drawable/home_btn_bg" android:state_pressed="true"></item>
  5. <item android:drawable="@drawable/home_btn_bg" android:state_selected="true"></item>
  6.  
  7. </selector>

bt_home_selector.xml(底部菜单按钮效果)

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3.  
  4. <item android:drawable="@drawable/icon_home_sel" android:state_selected="true"></item>
  5. <item android:drawable="@drawable/icon_home_nor"></item>
  6.  
  7. </selector>

FragmentPage1-FragmentPage5.java

  1. package com.example.newtabhosttest;
  2.  
  3. import android.os.Bundle;
  4. import android.support.annotation.Nullable;
  5. import android.support.v4.app.Fragment;
  6. import android.view.LayoutInflater;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9.  
  10. public class FragmentPage1 extends Fragment{
  11. @Override
  12. public View onCreateView(LayoutInflater inflater,
  13. @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
  14. return inflater.inflate(R.layout.fragment1, null);
  15. }
  16.  
  17. }

MainActivity.java(主代码)

  1. package com.example.newtabhosttest;
  2.  
  3. import android.os.Bundle;
  4. import android.support.v4.app.FragmentActivity;
  5. import android.support.v4.app.FragmentTabHost;
  6. import android.view.View;
  7. import android.widget.ImageView;
  8. import android.widget.TabHost.TabSpec;
  9. import android.widget.TextView;
  10.  
  11. public class MainActivity extends FragmentActivity {
  12.  
  13. private FragmentTabHost fragmentTabHost;
  14. private String texts[] = { "首页", "消息", "好友", "广场", "更多" };
  15. private int imageButton[] = { R.drawable.bt_home_selector,
  16. R.drawable.bt_message_selector, R.drawable.bt_selfinfo_selector,R.drawable.bt_square_selector ,R.drawable.bt_more_selector};
  17. private Class fragmentArray[] = {FragmentPage1.class,FragmentPage2.class,FragmentPage3.class,FragmentPage4.class,FragmentPage5.class};
  18.  
  19. @Override
  20. protected void onCreate(Bundle savedInstanceState) {
  21. super.onCreate(savedInstanceState);
  22. setContentView(R.layout.activity_main);
  23.  
  24. // 实例化tabhost
  25. fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
  26. fragmentTabHost.setup(this, getSupportFragmentManager(),
  27. R.id.maincontent);
  28.  
  29. for (int i = 0; i < texts.length; i++) {
  30. TabSpec spec=fragmentTabHost.newTabSpec(texts[i]).setIndicator(getView(i));
  31.  
  32. fragmentTabHost.addTab(spec, fragmentArray[i], null);
  33.  
  34. //设置背景(必须在addTab之后,由于需要子节点(底部菜单按钮)否则会出现空指针异常)
  35. fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bt_selector);
  36. }
  37.  
  38. }
  39.  
  40. private View getView(int i) {
  41. //取得布局实例
  42. View view=View.inflate(MainActivity.this, R.layout.tabcontent, null);
  43.  
  44. //取得布局对象
  45. ImageView imageView=(ImageView) view.findViewById(R.id.image);
  46. TextView textView=(TextView) view.findViewById(R.id.text);
  47.  
  48. //设置图标
  49. imageView.setImageResource(imageButton[i]);
  50. //设置标题
  51. textView.setText(texts[i]);
  52. return view;
  53. }
  54.  
  55. }

到这里代码就结束了,要是有哪里疑惑的朋友可以给我留言,如果觉得文章对您有用的话,请给个赞,谢谢支持!^_^

安卓开发笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)的更多相关文章

  1. 转-Fragment+FragmentTabHost组件(实现新浪微博底部菜单)

    http://www.cnblogs.com/lichenwei/p/3985121.html 记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果, ...

  2. 安卓开发笔记——Fragment+ViewPager组件(高仿微信界面)

    什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开发复习笔记——ViewPager组件(仿微信引导界面)>,不清楚的朋友可以看看,这里就不再 ...

  3. 安卓开发笔记——关于开源组件PullToRefresh实现下拉刷新和上拉加载(一分钟搞定,超级简单)

    前言 以前在实现ListView下拉刷新和上拉加载数据的时候都是去继承原生的ListView重写它的一些方法,实现起来非常繁杂,需要我们自己去给ListView定制下拉刷新和上拉加载的布局文件,然后添 ...

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

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

  5. 安卓开发笔记——自定义广告轮播Banner(实现无限循环)

    关于广告轮播,大家肯定不会陌生,它在现手机市场各大APP出现的频率极高,它的优点在于"不占屏",可以仅用小小的固定空位来展示几个甚至几十个广告条,而且动态效果很好,具有很好的用户& ...

  6. 安卓开发笔记——丰富多彩的TextView

    随手笔记,记录一些东西~ 记得之前写过一篇文章<安卓开发笔记——个性化TextView(新浪微博)>:http://www.cnblogs.com/lichenwei/p/4411607. ...

  7. 安卓开发笔记——关于开源项目SlidingMenu的使用介绍(仿QQ5.0侧滑菜单)

    记得去年年末的时候写过这个侧滑效果,当时是利用自定义HorizontalScrollView来实现的,效果如下: 有兴趣的朋友可以看看这篇文件<安卓开发笔记——自定义HorizontalScro ...

  8. 安卓开发笔记——深入Activity

    在上一篇文章<安卓开发笔记——重识Activity >中,我们了解了Activity生命周期的执行顺序和一些基本的数据保存操作,但如果只知道这些是对于我们的开发需求来说是远远不够的,今天我 ...

  9. Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)

    采取的方法是Fragment+FragmentTabHost组件来实现这种常见的app主页面的效果 首先给出main.xml文件 <?xml version="1.0" en ...

随机推荐

  1. Kafka、RabbitMQ、RocketMQ消息中间件的对比 —— 消息发送性能

    引言 分布式系统中,我们广泛运用消息中间件进行系统间的数据交换,便于异步解耦.现在开源的消息中间件有很多,前段时间我们自家的产品 RocketMQ (MetaQ的内核) 也顺利开源,得到大家的关注. ...

  2. Delphi调用JAVA的WebService上传XML文件(XE10.2+WIN764)

    相关资料:1.http://blog.csdn.net/luojianfeng/article/details/512198902.http://blog.csdn.net/avsuper/artic ...

  3. 解决maven项目 maven install失败 错误 Failed to execute goal org.apache.maven.plugins

    1.Maven构建失败 Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin: 2.3.4 :compile ( ...

  4. ubuntu 12.04下编译安装nginx-1.9.3

    1,下载nginx-1.9.3.tar.gz 两种方式: (1).ubuntu 下终端中(ctrl+alt+t) 运行命令: wget http://nginx.org/download/nginx- ...

  5. mac OS X中升级php5.5至php5.6 or php7

    在做php项目中,提示“Warning. PHP 5.6 or newer is required. Please, upgrade your local PHP installation.” 通过p ...

  6. laravel服务l队列资料整理

    Laravel 队列系列 —— 基于 Redis 实现任务队列的基本配置和使用 1.概述 在Web开发中,我们经常会遇到需要批量处理任务的场景,比如群发邮件.秒杀资格获取等,我们将这些耗时或者高并发的 ...

  7. CentOS6.3升级Python到2.7.3版本

    http://www.zhangchun.org/the-centos6-3-upgrade-python-to-2-7-3-version/ 查看python的版本 1 python  -V  2 ...

  8. Java编程的逻辑 (47) - 堆和PriorityQueue的应用

    本系列文章经补充和完善,已修订整理成书<Java编程的逻辑>,由机械工业出版社华章分社出版,于2018年1月上市热销,读者好评如潮!各大网店和书店有售,欢迎购买,京东自营链接:http:/ ...

  9. 使用SoapUI 对WebService压力测试

    SoapUI版本:5.0.0 测试步骤: 1.新建测试项目: 2.生成TestSuite以及LoadTest 以上操作完成以后项目如下: 开始测试: 双击LoadTest1,如下图: 点击左上角绿色三 ...

  10. NetBeans Lookups Explained!

    https://dzone.com/articles/netbeans-lookups-explained —————————————————————————————————————————————— ...