底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏
一、实现效果图


二、项目工程结构


三、详细代码编写
1、主tab布局界面,main_tab_layout:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
 | 
<?xml version="1.0" encoding="utf-8"?>      android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical" >             <FrameLayout        android:id="@+id/realtabcontent"        android:layout_width="fill_parent"        android:layout_height="0dip"        android:layout_weight="1" />             <android.support.v4.app.FragmentTabHost        android:id="@android:id/tabhost"        android:layout_width="fill_parent"        android:layout_height="wrap_content"        android:background="@drawable/maintab_toolbar_bg">                 <FrameLayout            android:id="@android:id/tabcontent"            android:layout_width="0dp"            android:layout_height="0dp"            android:layout_weight="0" />                  </android.support.v4.app.FragmentTabHost>         </LinearLayout> | 
2、Tab按钮选项布局,tab_item_view.xml:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
 | 
<?xml version="1.0" encoding="utf-8"?>      android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:gravity="center"    android:orientation="vertical" >             <ImageView        android:id="@+id/imageview"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:focusable="false"        android:padding="3dp"        android:src="@drawable/tab_home_btn">      </ImageView>             <TextView        android:id="@+id/textview"            android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:text="首页"        android:textSize="10sp"        android:textColor="#ffffff">      </TextView>         </LinearLayout> | 
3、fragment布局界面,这里只列出一个,fragment_1.xml:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
 | 
<?xml version="1.0" encoding="utf-8"?>      android:layout_width="fill_parent"    android:layout_height="fill_parent" >             <ImageView        android:id="@+id/imageview"        android:layout_width="fill_parent"        android:layout_height="fill_parent"        android:scaleType="fitCenter"        android:src="@drawable/xianjian01" >      </ImageView>         </LinearLayout> | 
4、Tab选项的自定义按钮资源文件,列出其中一个按钮,tab_home_btn:
| 
 1 
2 
3 
4 
5 
6 
7 
 | 
<?xml version="1.0" encoding="utf-8"?>             <item android:drawable="@drawable/icon_home_sel" android:state_selected="true"/>      <item android:drawable="@drawable/icon_home_nor"/>         </selector> | 
5、Tab选项按钮背景资源文件,selector_tab_background.xml:
| 
 1 
2 
3 
4 
5 
6 
7 
 | 
<?xml version="1.0" encoding="utf-8"?>             <item android:drawable="@drawable/home_btn_bg" android:state_pressed="true"/>      <item android:drawable="@drawable/home_btn_bg" android:state_selected="true"/>         </selector> | 
6、主Activity类,MainTabActivity.java:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
46 
47 
48 
49 
50 
51 
52 
53 
54 
55 
56 
57 
58 
59 
60 
61 
62 
63 
64 
65 
66 
67 
68 
69 
70 
71 
72 
73 
74 
75 
76 
77 
78 
 | 
package com.yangyu.mycustomtab02;         import android.os.Bundle;  import android.support.v4.app.FragmentActivity;  import android.support.v4.app.FragmentTabHost;  import android.view.LayoutInflater;  import android.view.View;  import android.widget.ImageView;  import android.widget.TabHost.TabSpec;  import android.widget.TextView;         /**  * @author yangyu  *  功能描述:自定义TabHost  */public class MainTabActivity extends FragmentActivity{        //定义FragmentTabHost对象      private FragmentTabHost mTabHost;                 //定义一个布局      private LayoutInflater layoutInflater;                     //定义数组来存放Fragment界面      private Class fragmentArray[] = {FragmentPage1.class,FragmentPage2.class,FragmentPage3.class,FragmentPage4.class,FragmentPage5.class};                 //定义数组来存放按钮图片      private int mImageViewArray[] = {R.drawable.tab_home_btn,R.drawable.tab_message_btn,R.drawable.tab_selfinfo_btn,                                       R.drawable.tab_square_btn,R.drawable.tab_more_btn};                 //Tab选项卡的文字      private String mTextviewArray[] = {"首页", "消息", "好友", "广场", "更多"};                 public void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main_tab_layout);                         initView();      }                  /**      * 初始化组件      */    private void initView(){          //实例化布局对象          layoutInflater = LayoutInflater.from(this);                                 //实例化TabHost对象,得到TabHost          mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);          mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);                          //得到fragment的个数          int count = fragmentArray.length;                                    for(int i = 0; i < count; i++){                //为每一个Tab按钮设置图标、文字和内容              TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i]).setIndicator(getTabItemView(i));              //将Tab按钮添加进Tab选项卡中              mTabHost.addTab(tabSpec, fragmentArray[i], null);              //设置Tab按钮的背景              mTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.selector_tab_background);          }      }                             /**      * 给Tab按钮设置图标和文字      */    private View getTabItemView(int index){          View view = layoutInflater.inflate(R.layout.tab_item_view, null);                     ImageView imageView = (ImageView) view.findViewById(R.id.imageview);          imageView.setImageResource(mImageViewArray[index]);                         TextView textView = (TextView) view.findViewById(R.id.textview);                  textView.setText(mTextviewArray[index]);                     return view;      }  } | 
7、Fragment页面,FragmentPage1.java:
| 
 1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
 | 
package com.yangyu.mycustomtab02;         import android.os.Bundle;  import android.support.v4.app.Fragment;  import android.view.LayoutInflater;  import android.view.View;  import android.view.ViewGroup;         public class FragmentPage1 extends Fragment{             @Override    public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {                return inflater.inflate(R.layout.fragment_1, null);           }     }  | 
底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏的更多相关文章
- 转-Fragment+FragmentTabHost组件(实现新浪微博底部菜单)
		
http://www.cnblogs.com/lichenwei/p/3985121.html 记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果, ...
 - 安卓开发笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)
		
记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果,但作为学习,还是需要来了解下这个新引入类FragmentTabHost 之前2篇文章的链接: 安 ...
 - Android自己定义TabActivity(实现仿新浪微博底部菜单更新UI)
		
现在Android上非常多应用都採用底部菜单控制更新的UI这样的框架,比如新浪微博 点击底部菜单的选项能够更新界面.底部菜单能够使用TabHost来实现,只是用过TabHost的人都知道自己定义Tab ...
 - Android 高仿新浪微博底部导航栏,实现双击首页Tab,页面的ListView滚动、刷新
		
现在很多APP,如微信.QQ.微博等等,它们的主页面都无一例外的选择使用底部Tab导航, 通过这种方式,可以很好的把页面层级分化,很好的提高用户体验.相信,很多Android开发者,都使用到过这种经典 ...
 - <Android 基础(三十三)> TabHost ~ 仿微信底部菜单
		
简介 Container for a tabbed window view. This object holds two children: a set of tab labels that the ...
 - Fragment+FragmentTabHost组件实现常见主页面(仿微信新浪)
		
采取的方法是Fragment+FragmentTabHost组件来实现这种常见的app主页面的效果 首先给出main.xml文件 <?xml version="1.0" en ...
 - ASP.NET仿新浪微博下拉加载更多数据瀑布流效果
		
闲来无事,琢磨着写点东西.貌似页面下拉加载数据,瀑布流的效果很火,各个网站都能见到各式各样的展示效果,原理大同小异.于是乎,决定自己写一写这个效果,希望能给比我还菜的菜鸟们一点参考价值. 在开始之前, ...
 - Android UI-实现底部切换标签(fragment)
		
Android UI-实现底部切换标签(fragment) 前言 本篇博客要分享的一个UI效果--实现底部切换标签,想必大家在一些应用上面遇到过这样的效果了,最典型的就是微信了,能够左右滑动切换页面. ...
 - IOS 使用SDWebImage实现仿新浪微博照片浏览器
		
使用第三方库SDWebImage实现仿新浪微博照片浏览器,可以下载图片缓存,点击之后滚动查看相片,具体效果如下: 代码如下: WeiboImageView.h: #import <UIKit/U ...
 
随机推荐
- 用19种编程语言写Hello World
			
用19种编程语言写Hello World 转载自:http://www.admin10000.com/document/394.html Hello World 程序是每一种编程语言最基本的程序,通常 ...
 - 有很多功能交给IIS实现就好了啊,之前干嘛要自己写?
			
比方说设置默认主页啊,错误页伪静态之类的
 - 工作总结:文件对话框的分类(C++)
			
原文地址:http://www.jizhuomi.com/software/173.html 文件对话框分为打开文件对话框和保存文件对话框,相信大家在Windows系统中经常见到这两种文件对话框.例如 ...
 - 制作PPT时,可能这些小习惯你需要注意
 - keybd_event函数用法
			
转自不用winio,直接达到驱动级模拟键盘效果的keybd_event函数用法 键盘模拟技术是在编写游戏外挂时经常使用的技术.但是由于很多游戏采用了directinput的方式,使得发送的一般键盘消息 ...
 - NAT(NAPT)地址转换过程
			
整理自NAT地址转换过程 注:本文实质讲的是NAPT(Network Address Port Translation),即网络端口地址转换.NAPT与动态地址NAT不同,它将内部连接映射到外部网络中 ...
 - QLGame 2D Engine编写环境搭建
			
QLGame 2D Engine编写 (win7环境搭建) 广州麒麟网络工作室,计划制作一款2d game engine,基于opengl(es)平台,暂时支持android,以后考虑支持linux, ...
 - 【UVA 11865】 Stream My Contest (二分+MDST最小树形图)
			
[题意] 你需要花费不超过cost元来搭建一个比赛网络.网络中有n台机器,编号0~n-1,其中机器0为服务器,其他机器为客户机.一共有m条可以使用的网线,其中第i条网线的发送端是机器ui,接收端是机器 ...
 - lubuntu安装maven
			
原文转自:jobar.iteye.com/blog/1776747 1 apt-cache search maven 获取所有可用的maven包 2 sudo apt-get install mave ...
 - [转贴]Linq之动态查询
			
最近写Linq碰到一个问题, 就是如果要写一个查询系统,这个系统里面有很多TextBox可以填, 然后捞出符合各个字段的数据. 在SQL查询的时候还满方便的,用字符串连连连就可以了, ( 也就是sql ...