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

之前2篇文章的链接:

安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航)

安卓开发复习笔记——TabHost组件(二)(实现底部菜单导航)

关于Fragment类在之前的安卓开发复习笔记——Fragment+ViewPager组件(高仿微信界面)也介绍过,这里就不再重复阐述了。

国际惯例,先来张效果图:

下面直接上代码了,注释很全,看过我前2篇文章的朋友,肯定秒懂的,哈哈~

activity_main.xml(主布局文件)

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" > <!-- 存放主要页面内容 --> <FrameLayout
android:id="@+id/maincontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout> <!-- 底层菜单 --> <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" >
</FrameLayout>
</android.support.v4.app.FragmentTabHost> </LinearLayout>

fragment.xml(由于只有文字不同,这里只给出一个)

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
> <TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="我是第一个Fragment"
android:textSize="20dp"
/> </RelativeLayout>

tabcontent.xml(具体底部菜单详细布局)

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" > <ImageView
android:id="@+id/image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
<TextView
android:id="@+id/text"
android:padding="2dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
/> </LinearLayout>

bt_selector.xml(底部菜单点击背景)

 <?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>
<item android:drawable="@drawable/home_btn_bg" android:state_selected="true"></item> </selector>

bt_home_selector.xml(底部菜单按钮效果)

 <?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>
<item android:drawable="@drawable/icon_home_nor"></item> </selector>

FragmentPage1-FragmentPage5.java

 package com.example.newtabhosttest;

 import android.os.Bundle;
import android.support.annotation.Nullable;
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,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, null);
} }

MainActivity.java(主代码)

 package com.example.newtabhosttest;

 import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView; public class MainActivity extends FragmentActivity { private FragmentTabHost fragmentTabHost;
private String texts[] = { "首页", "消息", "好友", "广场", "更多" };
private int imageButton[] = { R.drawable.bt_home_selector,
R.drawable.bt_message_selector, R.drawable.bt_selfinfo_selector,R.drawable.bt_square_selector ,R.drawable.bt_more_selector};
private Class fragmentArray[] = {FragmentPage1.class,FragmentPage2.class,FragmentPage3.class,FragmentPage4.class,FragmentPage5.class}; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 实例化tabhost
fragmentTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
fragmentTabHost.setup(this, getSupportFragmentManager(),
R.id.maincontent); for (int i = 0; i < texts.length; i++) {
TabSpec spec=fragmentTabHost.newTabSpec(texts[i]).setIndicator(getView(i)); fragmentTabHost.addTab(spec, fragmentArray[i], null); //设置背景(必须在addTab之后,由于需要子节点(底部菜单按钮)否则会出现空指针异常)
fragmentTabHost.getTabWidget().getChildAt(i).setBackgroundResource(R.drawable.bt_selector);
} } private View getView(int i) {
//取得布局实例
View view=View.inflate(MainActivity.this, R.layout.tabcontent, null); //取得布局对象
ImageView imageView=(ImageView) view.findViewById(R.id.image);
TextView textView=(TextView) view.findViewById(R.id.text); //设置图标
imageView.setImageResource(imageButton[i]);
//设置标题
textView.setText(texts[i]);
return view;
} }

到这里代码就结束了,要是有哪里疑惑的朋友可以给我留言,如果觉得文章对您有用的话,请给个赞,谢谢支持!^_^

安卓开发笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)的更多相关文章

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

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

  2. 安卓开发笔记——Fragment+ViewPager组件(高仿微信界面)

    什么是ViewPager? 关于ViewPager的介绍和使用,在之前我写过一篇相关的文章<安卓开发复习笔记——ViewPager组件(仿微信引导界面)>,不清楚的朋友可以看看,这里就不再 ...

  3. 安卓开发笔记——关于开源组件PullToRefresh实现下拉刷新和上拉加载(一分钟搞定,超级简单)

    前言 以前在实现ListView下拉刷新和上拉加载数据的时候都是去继承原生的ListView重写它的一些方法,实现起来非常繁杂,需要我们自己去给ListView定制下拉刷新和上拉加载的布局文件,然后添 ...

  4. 底部菜单栏(三)Fragment+FragmentTabHost实现仿新浪微博底部菜单栏

    一.实现效果图 二.项目工程结构 三.详细代码编写 1.主tab布局界面,main_tab_layout: 双击代码全选 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 ...

  5. 安卓开发笔记——自定义广告轮播Banner(实现无限循环)

    关于广告轮播,大家肯定不会陌生,它在现手机市场各大APP出现的频率极高,它的优点在于"不占屏",可以仅用小小的固定空位来展示几个甚至几十个广告条,而且动态效果很好,具有很好的用户& ...

  6. 安卓开发笔记——丰富多彩的TextView

    随手笔记,记录一些东西~ 记得之前写过一篇文章<安卓开发笔记——个性化TextView(新浪微博)>:http://www.cnblogs.com/lichenwei/p/4411607. ...

  7. 安卓开发笔记——关于开源项目SlidingMenu的使用介绍(仿QQ5.0侧滑菜单)

    记得去年年末的时候写过这个侧滑效果,当时是利用自定义HorizontalScrollView来实现的,效果如下: 有兴趣的朋友可以看看这篇文件<安卓开发笔记——自定义HorizontalScro ...

  8. 安卓开发笔记——深入Activity

    在上一篇文章<安卓开发笔记——重识Activity >中,我们了解了Activity生命周期的执行顺序和一些基本的数据保存操作,但如果只知道这些是对于我们的开发需求来说是远远不够的,今天我 ...

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

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

随机推荐

  1. hdu1598

    思路:对所有路径的速度从小到大排个序,然后枚举高度差就ok...... #include<iostream> #include<cstdio> #include<cstr ...

  2. 05-老马jQuery教程-动画

    前言 jQuery的动画系统做的非常出色,而且把最常用的显示.隐藏.淡入淡出.滑动显示和折叠凳效果都做了很好的封装.跟jQuery的选择器和事件配合起来,可以实现很多很绚的效果,而且简单易用兼容性好. ...

  3. Oracle数据库密码过期

    按照如下步骤进行操作:1.查看用户的proifle是哪个,一般是default: SQL>SELECT USERNAME,PROFILE FROM DBA_USERS; 2.查看指定概要文件(如 ...

  4. iOS 检测网络状态 自动判断 认为提示网络改变

    检测网络状态 在网络应用中,需要对用户设备的网络状态进行实时监控,目的是让用户了解自己的网络状态,防止一些误会(比如怪应用无能)根据用户的网络状态进行智能处理,节省用户流量,提高用户体验WIFI\3G ...

  5. 【转】mybatis 自增主键配置

    mybatis自增主键配置(?) mybatis进行插入操作时,如果表的主键是自增的,针对不同的数据库相应的操作也不同.基本上经常会遇到的就是Oracle Sequece 和 MySQL 自增主键,至 ...

  6. 微服务之springCloud和docker-Eureka(一)

    前言    本文记录怎么搭建服务注册中心eureka,然后打包成docker镜像,用docker swarm部署eureka集群 1.环境 环境 版本及说明 参考地址 docker v1.13.1,D ...

  7. python入门-分类和回归各种初级算法

    引自:http://www.cnblogs.com/taichu/p/5251332.html ########################### #说明: # 撰写本文的原因是,笔者在研究博文“ ...

  8. -Dmaven.multiModuleProjectDirectory system property is not set. Check $M2_HOME environment variable and mvn script match.

    在执行[maven clean]的时候报错,错误信息如下: -Dmaven.multiModuleProjectDirectory system property is not set. Check ...

  9. Python之2维list转置、旋转及其简单应用

    给一个矩阵,顺时针旋转顺序输出其元素,例如: 对于矩阵: [ 1, 2, 3 ] [ 4, 5, 6 ] [ 7, 8, 9 ] 输出为: 1,2,3,6,9,8,7,4,5 def transpos ...

  10. mysql数据库性能测试报告

    网上有写的好的模板, 直接复链接了: 1. http://blog.csdn.net/mituan1234567/article/details/45247989 2. https://msdn.mi ...