记得之前写过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;
} }

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

最新文章

  1. Sharp Memory LCD (ls013b7dh03)驱动
  2. 体验 ASP.NET Core 1.1 中预编译 MVC Razor 视图
  3. java虚拟机之垃圾回收算法
  4. mvc+mysql+EF
  5. 采用软件nginx实现web服务器集群
  6. HW4.15
  7. du和df不一致的解决方法
  8. 相比于python2.6,python3.0的新特性。
  9. ubuntu12.04安装深度音乐播放器和深度影音
  10. thinkphp 中英文网站详解
  11. Spring事务管理总结
  12. 在Django中使用ORM创建图书管理系统
  13. SqlServer导入Excel数据
  14. python爬虫笔记----4.Selenium库(自动化库)
  15. hdu 5248 贪心
  16. Android -- 触摸Area对焦区域(更新)
  17. removeChildByTag、schedule、schedule_selector
  18. UIView动画补充
  19. Mybatis笔记四:nested exception is org.apache.ibatis.reflection.ReflectionException: There is no getter for property named &#39;id&#39; in &#39;class java.lang.String&#39;
  20. 微信小程序开发之路之组件化

热门文章

  1. SQL学习(持续更新)
  2. Android开发(三十)——ScrollView中ListView的高度自动适应
  3. postgresql相关命令
  4. C#学习笔记(24)——C#将PPT批量转为JPG(最简单的方法)
  5. cacti监控jvm
  6. C语言 &#183; 和最大子序列
  7. python入门-分类和回归各种初级算法
  8. JavaScript(五):变量的作用域
  9. 三篇文章了解 TiDB 技术内幕——说计算
  10. 微信SDK 报错 invalid url domanin