一、实现效果图

二、项目工程结构

三、详细代码编写

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"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    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"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"
       
    <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"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"
       
    <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实现仿新浪微博底部菜单栏的更多相关文章

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

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

  2. 安卓开发笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)

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

  3. Android自己定义TabActivity(实现仿新浪微博底部菜单更新UI)

    现在Android上非常多应用都採用底部菜单控制更新的UI这样的框架,比如新浪微博 点击底部菜单的选项能够更新界面.底部菜单能够使用TabHost来实现,只是用过TabHost的人都知道自己定义Tab ...

  4. Android 高仿新浪微博底部导航栏,实现双击首页Tab,页面的ListView滚动、刷新

    现在很多APP,如微信.QQ.微博等等,它们的主页面都无一例外的选择使用底部Tab导航, 通过这种方式,可以很好的把页面层级分化,很好的提高用户体验.相信,很多Android开发者,都使用到过这种经典 ...

  5. <Android 基础(三十三)> TabHost ~ 仿微信底部菜单

    简介 Container for a tabbed window view. This object holds two children: a set of tab labels that the ...

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

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

  7. ASP.NET仿新浪微博下拉加载更多数据瀑布流效果

    闲来无事,琢磨着写点东西.貌似页面下拉加载数据,瀑布流的效果很火,各个网站都能见到各式各样的展示效果,原理大同小异.于是乎,决定自己写一写这个效果,希望能给比我还菜的菜鸟们一点参考价值. 在开始之前, ...

  8. Android UI-实现底部切换标签(fragment)

    Android UI-实现底部切换标签(fragment) 前言 本篇博客要分享的一个UI效果--实现底部切换标签,想必大家在一些应用上面遇到过这样的效果了,最典型的就是微信了,能够左右滑动切换页面. ...

  9. IOS 使用SDWebImage实现仿新浪微博照片浏览器

    使用第三方库SDWebImage实现仿新浪微博照片浏览器,可以下载图片缓存,点击之后滚动查看相片,具体效果如下: 代码如下: WeiboImageView.h: #import <UIKit/U ...

随机推荐

  1. C# 两个获得程序运行路径的函数

    EXE文件的存储路径,不太受调用时环境变量的影响: Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Lo ...

  2. Codeforces Round #198 (Div. 2) —— A

    最水的题,可惜当时赶时间没有注意数据范围:暴力超时了! 其实应该用x,y的最大公约数来判断: 代码: #include<iostream> using namespace std; int ...

  3. Ubuntu配置apache

    http://blog.csdn.net/ljchlx/article/details/21978431 http://www.2cto.com/os/201110/107283.html

  4. TortoiseSVN下载,安装,配置,常用操作 svn教程

    一. 首先在百度搜索并下载 TortoiseSVN 推荐从官网下载,软件分为32位和64位版本,下载时请根据自己的系统位数进行下载:

  5. 单片机 C 语言模块化编程

    好的开始是成功的一半 通过上一章的学习,我想你已经掌握了如何在程序中释放CPU了.希望能够继续坚持下去.一个良好的开始是成功的一半.我们今天所做的一切都是为了在单片机编程上做的更好. 在谈论今天的主题 ...

  6. 使用GO语言灵活批量ssh登录服务器执行操作

    摘要: 在工作中时常需要登录服务器做一系列操作,每次输入ssh xxx总是很麻烦.这时候为什么不考虑写一个通用的小脚本呢? go语言是一门新兴语言,能够在很多地方发挥总用.初学go语言,做了这么一个小 ...

  7. java.lang.String内部结构的变化

    原文:http://java-performance.info/changes-to-string-java-1-7-0_06/ 作者:Mikhail Vorontsov IMPORTANT: Jav ...

  8. struts2结合生成验证码

    import java.util.Arrays; /** * 工具类,生成随机验证码字符串 * @version 1.0 2012/12/01 * @author shiyz * */ public ...

  9. IBinder类的中文翻译

    远程对象的基础接口,是一个为了在执行进程中和进程间调用时的高性能,而设计的轻量级远程调用机制的核心部分.这个接口描述了和远程对象交互的抽象协议.不要直接实现这个接口,而是通过继承Binder来实现. ...

  10. pcDuino汉化方法

    1,打开终端:2,在终端输入命令 sudo apt-get update 更新一下软件源3, 输入命令下载中文支持包 sudo apt-get install language-pack-gnome- ...