本文来自肥宝传说之路,引用必须注明出处!

这章憋了好久。本来想写选项卡的,学到TabHost,TabWidget的,把代码拿过来准备研究的时候,发现竟然在4.0.3版本号被废弃了。

百度一下,发如今后面的版本号,用FragmentTabHost和LayoutInflater来取代了。网上也有一些关于Frame的内容,可是都不是新手教程的。

写得不够通俗。想直接拿代码下来研究,发现竟然非常多人都是上传代码片段,然后再给个收费链接。作为一个穷屌丝,仅仅能自己一点一点去研究了。

Frament是什么?直译是片段的意思,Frament是为了解决Android同一套代码在不同尺寸屏幕的设备上显示的问题而诞生的,是Activity的一个部分。

事实上就是把界面分成一部分一部分的方便管理。更深入的了解能够看这里点击打开链接

FragmentActivity 继承自Activity。可以使用Frament相关内容的Activity。

FragmentTabHost 替代了TabHost的类

来,先看个图:

呃。。

。MacBook的图就是打。

。。

边看代码边说吧:

activity_hello_world.xml 主界面的布局文件,使用了帧布局,将菜单条和页面发在一起,同一时候显示

<?

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/bottom_bar"> <FrameLayout
android:id="@android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0" />
</android.support.v4.app.FragmentTabHost> </LinearLayout>

我们能够看出,菜单条须要一个布局文件来控制这些button的摆放。同一时候,选项卡页面内,也是须要有一个布局文件。

tab_item_view.xml   tabbutton的布局文件。button由图片ImageView和文字TextView两部分组成

<?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/bottom_bar">
</ImageView> <TextView
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="12sp"
android:textColor="#FFFFFF">
</TextView> </LinearLayout>

fragment1.xml   页面fragment的布局文件 C1FFC1是背景颜色。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#C1FFC1" >
</LinearLayout>

fragment2.xml   这里仅仅是颜色不同而已

  android:background="#EEEE00"

fragment3.xml   这里仅仅是颜色不同而已

  android:background="#FFFFFF"

fragment4.xml   这里仅仅是颜色不同而已

  android:background="#C6555D"

fragment5.xml   这里仅仅是颜色不同而已

  android:background="#000000"

Fragment1.java 就读一下布局文件

package com.fable.helloworld;

import com.fable.helloworld.R;
import com.fable.helloworld.R.layout; import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; public class Fragment1 extends Fragment{ @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment1, null); //fragment2345事实上也就是这里不同而已。
}
}

HelloWorldActivity.java 主要逻辑实现

package com.fable.helloworld;

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; import com.fable.helloworld.Fragment1;
import com.fable.helloworld.R; public class HelloWorldActivity extends FragmentActivity { //FragmentActivity 能够对Fragment进行操作的Activity private FragmentTabHost mTabHost; //布局填充器
private LayoutInflater mLayoutInflater; //Fragment数组界面
private Class mFragmentArray[] = { Fragment1.class, Fragment2.class,
Fragment3.class, Fragment4.class, Fragment5.class }; //存放图片数组
private int mImageArray[] = { R.drawable.home,
R.drawable.ic_dialog_email, R.drawable.ic_menu_my_calendar,
R.drawable.ic_search_category_default, R.drawable.ic_input_add }; //选项卡文字
private String mTextArray[] = { "首页", "消息", "好友", "搜索", "很多其它" }; public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_world); initView();//初始化视图
} /**
* 初始化组件
*/
private void initView() {
mLayoutInflater = LayoutInflater.from(this);//布局填充,动态布局用到 mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);// 找到TabHost选项卡
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);//选项卡容器
// 得到fragment的个数
int count = mFragmentArray.length;
for (int i = 0; i < count; i++) {
// 给每一个Tabbutton设置图标、文字和内容
TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i]).setIndicator(getTabItemView(i));
// 将Tabbutton加入进Tab选项卡中
mTabHost.addTab(tabSpec, mFragmentArray[i], null);//第二个參数就是选项卡相应页面的详细内容
// 设置Tabbutton的背景
mTabHost.getTabWidget().getChildAt(i)
.setBackgroundResource(R.drawable.bottom_bar);
}
} /**
*
* 给每一个Tabbutton设置图标和文字
*/
private View getTabItemView(int index) {
View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);//tab的动态布局
ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
imageView.setImageResource(mImageArray[index]);//设置图标
TextView textView = (TextView) view.findViewById(R.id.textview);
textView.setText(mTextArray[index]);//设置文字
//这里能够看出,往一个视图view里面插东西。就是先通过id找出里面元素对象,然后在对对象进行操作
return view;
}
}

代码不多。但文件多了一点,所以上传了代码,点击打开链接

Android新手入门2016(14)--FragmentTabHost实现选项卡和菜单的更多相关文章

  1. Android新手入门2016(7)--布局

    布局,这个在服务端变成是没有的,也是服务端的人学习client的一道坎吧. 曾经用cocos2d-x写小游戏的时候就是这个非常难懂,或者能用,可是理解不多的话,非常难写出好的布局,难以适合商业化的应用 ...

  2. Android新手入门2016(10)--GridView

    本文来自肥宝传说之路.引用必须注明出处! GridView跟ListView一样是多控件布局.实现九宫图是最方便的. 还是先看看图,没图说个鸡鸡是不是 如上图.是一种应用方式.在每一个格子里面.放入应 ...

  3. Android新手入门2016(8)--ListView之ArrayAdapter

    本文来自肥宝传说之路,引用必须注明出处! ListView是Android中经常使用的控件. 什么是列表视图,让我们先看看图: watermark/2/text/aHR0cDovL2Jsb2cuY3N ...

  4. Android新手入门

    本博客出自公众号安卓应用频道:http://mp.weixin.qq.com/s?__biz=MzA3MDMyMjkzNg==&mid=2652261947&idx=1&sn= ...

  5. eclipse再见,android studio 新手入门教程(一)基本设置

    写在前面: 作为一个刚半只脚踏入android开发的新手,在使用eclipse开发了两个自我感觉不甚成熟的商城类app之后,遇到了一些问题,总结为如下: 代码复用性.findviewById,oncl ...

  6. 《IM开发新手入门一篇就够:从零开发移动端IM》

        登录 立即注册 TCP/IP详解 资讯 动态 社区 技术精选 首页   即时通讯网›专项技术区›IM开发新手入门一篇就够:从零开发移动端IM   帖子 打赏 分享 发表评论162     想开 ...

  7. 安卓自动化测试(2)Robotium环境搭建与新手入门教程

    Robotium环境搭建与新手入门教程 准备工具:Robotium资料下载 知识准备: java基础知识,如基本的数据结构.语法结构.类.继承等 对Android系统较为熟悉,了解四大组件,会编写简单 ...

  8. [译]:Xamarin.Android开发入门——Hello,Android快速上手

    返回索引目录 原文链接:Hello, Android_Quickstart. 译文链接:Xamarin.Android开发入门--Hello,Android快速上手 本部分介绍利用Xamarin开发A ...

  9. 原创:从零开始,微信小程序新手入门宝典《一》

    为了方便大家了解并入门微信小程序,我将一些可能会需要的知识,列在这里,让大家方便的从零开始学习:一:微信小程序的特点张小龙:张小龙全面阐述小程序,推荐通读此文: 小程序是一种不需要下载.安装即可使用的 ...

随机推荐

  1. Eclipse下的java工程目录问题和路径问题理解

    1.Eclipse下的java工程都有哪些文件夹? 答:new java project时,会默认创建SRC源代码目录,并默认创建一个bin目录作为输出目录,输出目录是指生成的class文件和配置文件 ...

  2. android:px,dp(dip),sp的差别

    1.px:表示屏幕的实际像素,比如320*480的屏幕在横向有320个像素,在纵向有480个像素,假设指定的某个空间的单位为px.那么在不同分辨率下的手机上.显示的都是指定的大小.一般不推荐使用px. ...

  3. 混合高斯模型的EM求解(Mixtures of Gaussians)及Python实现源代码

    今天为大家带来混合高斯模型的EM推导求解过程. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQveHVhbnl1YW5zZW4=/font/5a6L5L2T/ ...

  4. 带输出參数的存储过程的定义,以及在aso.net中调用

    ALTER proc [dbo].[mp_w_RechargePortalPayPal_All] ( @PayPalOrderNo nvarchar(50), --订单号 @nAccountIDFro ...

  5. 2)Win10-UWA开发 API參考 - 1

    孙广东  2015.8.23 大多数 Windows 执行时 API 如今适用于 Windows Phone 应用商店应用以及 Windows 应用商店应用,这意味着当你创建同一时候面向 Window ...

  6. 上传文件 nginx 413错误

    nginx : 413 Request Entity Too Large 上传文件过程发生413 Request Entity Too Large错误,翻译为请求实体过大,断定为nginx限制了请求体 ...

  7. hdoj 4548 美素数 【打表】

    另类打表:将从1到n的满足美素数条件的数目赋值给prime[n],这样最后仅仅须要用prime[L]减去prime[R-1]就可以: 美素数 Time Limit: 3000/1000 MS (Jav ...

  8. select into in mysql

    http://stackoverflow.com/questions/16809393/select-into-in-mysql Use the CREATE TABLE SELECT syntax. ...

  9. 4K 对齐与固态硬盘检测工具

    0. 硬盘扇区 当前电脑传统机械硬盘的每个扇区一般大小为 512 字节(512B):当使用某一文件系统将硬盘格式化时,文件系统会将硬盘扇区.磁道与柱面统计整理并定义一个簇为多少扇区方便快速存储. 现时 ...

  10. 搞笑OI

    OI难 噫吁嚱,维护难哉!OI之难,难于上青天!哈希及DP,代码何茫然!尔来一千两百A,不见金牌背后难.西当华师有考场,可以横绝CN巅.编译不过壮士死,然后超时爆内存相钩连.上有自主招生之高标,下有由 ...