转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/9016223      

在上一篇文章中,我们花了大量的篇幅来讲解Fragment这个新引进类的使用,目的就是为了让大家能够牢牢的掌握它的使用方法,以便读者在今后的开发中能够熟练的使用它。

一、实现效果图


二、项目工程结构

三、详细代码编写

1、主tab布局界面,main_tab_layout:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:orientation="vertical" >
  6. <FrameLayout
  7. android:id="@+id/realtabcontent"
  8. android:layout_width="fill_parent"
  9. android:layout_height="0dip"
  10. android:layout_weight="1" />
  11. <android.support.v4.app.FragmentTabHost
  12. android:id="@android:id/tabhost"
  13. android:layout_width="fill_parent"
  14. android:layout_height="wrap_content"
  15. android:background="@drawable/maintab_toolbar_bg">
  16. <FrameLayout
  17. android:id="@android:id/tabcontent"
  18. android:layout_width="0dp"
  19. android:layout_height="0dp"
  20. android:layout_weight="0" />
  21. </android.support.v4.app.FragmentTabHost>
  22. </LinearLayout>

2、Tab按钮选项布局,tab_item_view.xml:

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="wrap_content"
  4. android:layout_height="wrap_content"
  5. android:gravity="center"
  6. android:orientation="vertical" >
  7. <ImageView
  8. android:id="@+id/imageview"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. android:focusable="false"
  12. android:padding="3dp"
  13. android:src="@drawable/tab_home_btn">
  14. </ImageView>
  15. <TextView
  16. android:id="@+id/textview"
  17. android:layout_width="wrap_content"
  18. android:layout_height="wrap_content"
  19. android:text="首页"
  20. android:textSize="10sp"
  21. android:textColor="#ffffff">
  22. </TextView>
  23. </LinearLayout>

3、fragment布局界面,这里只列出一个,fragment_1.xml:

[html] view plaincopy
  1. <span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent" >
  5. <ImageView
  6. android:id="@+id/imageview"
  7. android:layout_width="fill_parent"
  8. android:layout_height="fill_parent"
  9. android:scaleType="fitCenter"
  10. android:src="@drawable/xianjian01" >
  11. </ImageView>
  12. </LinearLayout></span>

4、Tab选项的自定义按钮资源文件,列出其中一个按钮,tab_home_btn:

[html] view plaincopy
  1. <span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@drawable/icon_home_sel" android:state_selected="true"/>
  4. <item android:drawable="@drawable/icon_home_nor"/>
  5. </selector></span>

5、Tab选项按钮背景资源文件,selector_tab_background.xml:

[html] view plaincopy
  1. <span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">
  3. <item android:drawable="@drawable/home_btn_bg" android:state_pressed="true"/>
  4. <item android:drawable="@drawable/home_btn_bg" android:state_selected="true"/>
  5. </selector></span>

6、主Activity类,MainTabActivity.java:

[java] view plaincopy
  1. <span style="font-size:12px;">package com.yangyu.mycustomtab02;
  2. import android.os.Bundle;
  3. import android.support.v4.app.FragmentActivity;
  4. import android.support.v4.app.FragmentTabHost;
  5. import android.view.LayoutInflater;
  6. import android.view.View;
  7. import android.widget.ImageView;
  8. import android.widget.TabHost.TabSpec;
  9. import android.widget.TextView;
  10. /**
  11. * @author yangyu
  12. *  功能描述:自定义TabHost
  13. */
  14. public class MainTabActivity extends FragmentActivity{
  15. //定义FragmentTabHost对象
  16. private FragmentTabHost mTabHost;
  17. //定义一个布局
  18. private LayoutInflater layoutInflater;
  19. //定义数组来存放Fragment界面
  20. private Class fragmentArray[] = {FragmentPage1.class,FragmentPage2.class,FragmentPage3.class,FragmentPage4.class,FragmentPage5.class};
  21. //定义数组来存放按钮图片
  22. private int mImageViewArray[] = {R.drawable.tab_home_btn,R.drawable.tab_message_btn,R.drawable.tab_selfinfo_btn,
  23. R.drawable.tab_square_btn,R.drawable.tab_more_btn};
  24. //Tab选项卡的文字
  25. private String mTextviewArray[] = {"首页", "消息", "好友", "广场", "更多"};
  26. public void onCreate(Bundle savedInstanceState) {
  27. super.onCreate(savedInstanceState);
  28. setContentView(R.layout.main_tab_layout);
  29. initView();
  30. }
  31. /**
  32. * 初始化组件
  33. */
  34. private void initView(){
  35. //实例化布局对象
  36. layoutInflater = LayoutInflater.from(this);
  37. //实例化TabHost对象,得到TabHost
  38. mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
  39. mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
  40. //得到fragment的个数
  41. int count = fragmentArray.length;
  42. for(int i = 0; i < count; i++){
  43. //为每一个Tab按钮设置图标、文字和内容
  44. TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));
  45. //将Tab按钮添加进Tab选项卡中
  46. mTabHost.addTab(tabSpec, fragmentArray[i], null);
  47. //设置Tab按钮的背景
  48. mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);
  49. }
  50. }
  51. /**
  52. * 给Tab按钮设置图标和文字
  53. */
  54. private View getTabItemView(int index){
  55. View view = layoutInflater.inflate(R.layout.tab_item_view, null);
  56. ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
  57. imageView.setImageResource(mImageViewArray[index]);
  58. TextView textView = (TextView) view.findViewById(R.id.textview);
  59. textView.setText(mTextviewArray[index]);
  60. return view;
  61. }
  62. }</span>

7、Fragment页面,FragmentPage1.java:

[java] view plaincopy
  1. <span style="font-size:12px;">package com.yangyu.mycustomtab02;
  2. import android.os.Bundle;
  3. import android.support.v4.app.Fragment;
  4. import android.view.LayoutInflater;
  5. import android.view.View;
  6. import android.view.ViewGroup;
  7. public class FragmentPage1 extends Fragment{
  8. @Override
  9. public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
  10. return inflater.inflate(R.layout.fragment_1, null);
  11. }
  12. }
  13. </span>
代码下载地址

【Android UI设计与开发】第08期:底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏相关推荐

  1. 【Android UI设计与开发】3.引导界面(三)实现应用程序只启动一次引导界面

    大部分的引导界面基本上都是千篇一律的,只要熟练掌握了一个,基本上也就没什么好说的了,要想实现应用程序只启动一次引导界面这样的效果,只要使用SharedPreferences类,就会让程序变的非常简单, ...

  2. 【Android UI设计与开发】9:滑动菜单栏(一)开源项目SlidingMenu的使用和示例-转...

    一.SlidingMenu简介 相信大家对SlidingMenu都不陌生了,它是一种比较新的设置界面或配置界面的效果,在主界面左滑或者右滑出现设置界面效果,能方便的进行各种操作.很多优秀的应用都采用了 ...

  3. 【Android UI设计与开发】第02期:引导界面(二)使用ViewPager实现欢迎引导页面

    转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/8980917 本系列文章都会以一个程序的实例开发为主线来进行讲解,以求达到一个 ...

  4. android ui设计与开发工具,Android用户体验与UI设计

    Android用户体验与UI设计 编辑 锁定 讨论 上传视频 本词条缺少概述图,补充相关内容使词条更完整,还能快速升级,赶紧来编辑吧! 本书是一部介绍Android用户体验.UI设计理念和方法论的作品 ...

  5. 【Android UI设计与开发】第05期:引导界面(五)实现应用程序只启动一次引导界面

    转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/8987342  这篇文章算是对整个引导界面开发专题的一个终结了吧,个人觉得大部 ...

  6. 【Android UI设计与开发】第01期:引导界面(一)ViewPager介绍和使用详解

    转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/8980917 做Android开发加起来差不多也有一年多的时间了,总是想写点自 ...

  7. 【Android UI设计与开发】第16期:滑动菜单栏(一)

    这期博主要给大家带来的是关于滑动菜单栏的实现效果. 一.SlidingMenu简介 相信大家对SlidingMenu都不陌生了,它是一种比较新的设置界面或配置界面的效果,在主界面左滑或者右滑出现设置界 ...

  8. 【Android UI设计与开发】第07期:底部菜单栏(二)Fragment的详细介绍和使用方法

    转载请注明出处:http://blog.csdn.net/yangyu20121224/article/details/8995025 由于TabActivity在Android4.0以后已经被完全弃 ...

  9. 【转】【Android UI设计与开发】第07期:底部菜单栏(二)Fragment的详细介绍和使用方法...

    原始地址:http://blog.csdn.net/yangyu20121224/article/category/1431917/1 由于TabActivity在Android4.0以后已经被完全弃 ...

最新文章

  1. iOS Webview打开不受信的URL
  2. 【前沿】MIT搞了个进取型机器人!能研究学习对象操纵的基础
  3. DeFi 史上最大盗窃案:一个漏洞盗走价值 6 亿美元资产?现已归还近一半
  4. Jackson 读写 JSON
  5. list中抽出某一个字段的值_Java的stream代替List解决单线程等问题
  6. python3默认编码_python3的url编码和解码,自定义gbk、utf-8的例子
  7. 三维建模:方法之CSG与B-Rep比较
  8. C++继承和组合——带你读懂接口和mixin,实现多功能自由组合
  9. ExtJs之Ext.data.Store
  10. 数据 3 分钟 | 《数据安全法》即将施行;ACM SIGMOD在西安举办;艾瑞咨询发布数据库行业报告...
  11. Cocos2dx游戏源码合集
  12. 使用 Azure CLI 创建 Windows 虚拟机
  13. 实战Javascript:结合电商主界面实现轮播图和倒计时秒杀
  14. 如何让插入PPT的音乐跨幻灯片后同时播放
  15. 国内的健身房管理系统榜单-捷径系统
  16. 文本密度 php,基于最大文本密度的网页正文抽取方法
  17. pycharm个人最喜欢的配色方案
  18. 鸿蒙只是电视机,荣耀智慧屏首发抢先体验:电视只是小功能,鸿蒙系统才是真亮点...
  19. android 视频缓存溢出导致视频黑屏,MediaMuxer+MediaCodec生成MP4视频黑屏
  20. Normalized Mutual information

热门文章

  1. webpack 采坑(CleanWebpackPlugin 插件报错: CleanWebpackPlugin is not a constructor ;)
  2. Nvidia推边缘运算平台EGX 未来将提供企业AI模型参考框架
  3. 一个简单的生产消费者示例
  4. linux免交互登陆远程主机并执行命令(密钥对和Expect)
  5. 使用敏捷回顾实施组织变革
  6. [Asp.net]使用flexpaper+swftools大文件分页转换实现在线预览
  7. Javascript学习笔记2——函数
  8. 地铁时光机第一阶段冲刺六
  9. 2018-07-09--记录一次gitlab迁移事件及遇到的问题
  10. 【python】命令行解析工具getopt用法