一、实现效果图

二、项目工程结构

三、详细代码编写

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. bzoj 1195: [HNOI2006]最短母串 爆搜

    1195: [HNOI2006]最短母串 Time Limit: 10 Sec  Memory Limit: 32 MBSubmit: 894  Solved: 288[Submit][Status] ...

  2. bzoj 3858: Number Transformation 暴力

    3858: Number Transformation Time Limit: 1 Sec  Memory Limit: 64 MBSubmit: 82  Solved: 41[Submit][Sta ...

  3. 12306 订票助手 C# 版

    闲着没事,也用C#写了一个12306的订票助手,虽然可能会有些BUG但是也能正常使用了下载地址:http://www.fishlee.net/soft/12306_helper/ 查票窗口,可以查询余 ...

  4. django ORM中update_or_create功能,如果只要匹配某一特定字段呢

    今天发现的需求,在官方文档找到说法: In English, that means start with any non-'defaults' keyword argument that doesn’ ...

  5. Https协议:SSL建立过程分析(也比较清楚,而且有OpenSSL的代码)

    web访问的两种方式: http协议,我们一般情况下是通过它访问web,因为它不要求太多的安全机制,使用起来也简单,很多web站点也只支持这种方式下的访问. https协议(Hypertext Tra ...

  6. [cocos2dx]计算scrollview元素的index

    scrollview的原生代码没有提供元素对齐功能 通过下面介绍的index计算方法以及scrollview自带的设置位置方法 void setContentOffsetInDuration(CCPo ...

  7. 【HDOJ】2159 FATE

    DP+贪心优化. #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXNUM ...

  8. java学习之线程

    一.线程总述: 线程是java当中一个重要的内容,如果想说线程的话,那我们应该先来讲一下什么是进程. 进程:那么什么是进程呢,进程从字面上来理解就是,正在进行的程序.就比如说我们在windows当中打 ...

  9. 深入详解SQL中的Null

    深入详解SQL中的Null NULL 在计算机和编程世界中表示的是未知,不确定.虽然中文翻译为 “空”, 但此空(null)非彼空(empty). Null表示的是一种未知状态,未来状态,比如小明兜里 ...

  10. bzoj1034

    贪心 尽可能让最强的赢,最弱的赢,都不行则最弱打最强 感性的想,我肯定要尽可能的赢,而且赢的要对等 实在不能赢就拿最小的拼,所谓的田忌赛马策略 由于总分一定,己方最差即己方最好时对方的分数 ..] o ...