一、实现效果图

二、项目工程结构

三、详细代码编写

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. delphi xe5 android 手机上使用sqlite

    本篇我们介绍一下在android手机上怎样使用sqlite数据库,这里用Navigator实现 增删改查. 1.新建firemonkey mobile application 2.选择blank ap ...

  2. structDemo1

    structDemo1 # include <iostream.h> # include <malloc.h> enum EType{ One = ,Tow,Three }; ...

  3. [原博客] BZOJ 2242 [SDOI2011] 计算器

    题目链接 noip级数论模版题了吧.让求三个东西: 给定y,z,p,计算`Y^Z Mod P` 的值. 给定y,z,p,计算满足`xy≡ Z ( mod P )`的最小非负整数. 给定y,z,p,计算 ...

  4. ECMall关于数据查询缓存的问题

    刚接触Ecmall的二次开发不久,接到一个任务.很常见的任务,主要是对数据库进行一些操作,其中查询的方法我写成这样: 01 function get_order_data($goods_id) 02 ...

  5. Python Web 性能和压力测试 multi-mechanize

    http://www.aikaiyuan.com/5318.html 对Web服务做Performance & Load测试,最常见的工具有Apache Benchmark俗称ab和商用工具L ...

  6. HDU 3308 LCIS 线段树区间更新

    最近开始线段树一段时间了,也发现了不少大牛的博客比如HH大牛  ,小媛姐.这个题目是我在看HH大牛的线段树专题是给出的习题,(可以去他博客找找,真心推荐)原本例题是POJ3667 Hotel 这个题目 ...

  7. [转贴]C编译过程概述

    http://my.oschina.net/apeng/blog/105245 C 编译过程概述 目前Linux下最常用的C语言编译器是GCC(GNU Compiler Collection),它是G ...

  8. 2016年如果还没有关注这些机器人公司,你就out了

    芯师爷语据 知名市场研究机构IDC发布报告称,预计到2019年,全球机器人及相关服务上的投入将达到1350亿美元,较2015年的710亿美元增长近一倍.报告称,机器人相关投资预计将以每年17%的速度增 ...

  9. 【Xamarin挖墙脚系列:Xamarin.IOS的程序的结构】

    原文:[Xamarin挖墙脚系列:Xamarin.IOS的程序的结构] 开始熟悉Xamarin在开发IOS的结构!!!!!!! 先看官方 这个是以一个单页面的程序进行讲述的. 1 程序引用的程序集,核 ...

  10. Android用户界面 UI组件--TextView及其子类(四) Chronometer计时器

    Chronometer是一个简单的定时器,你可以给它一个开始时间,并以此定时,或者如果你不给它一个开始时间,它将会使用你的时间通话开始.默认情况下它会显示在当前定时器的值的形式“分:秒”或“H:MM: ...