Android新手入门2016(14)--FragmentTabHost实现选项卡和菜单

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

这章憋了好久。本来想写选项卡的,学到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;
	  }
}

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

时间: 2024-08-05 12:06:27

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

Android新手入门2016(16)--画图

本文来自肥宝传说之路,引用必须注明出处! 画图设计到图片的格式,有空可以看看图片资源各种格式.了解一下图片格式,对学习有用的.而且我面试别人的时候也很喜欢问这个问题,哈哈. 先看个图. 直接看代码吧,注释很详细了. activity_hello_world.xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.andro

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

布局,这个在服务端变成是没有的,也是服务端的人学习client的一道坎吧. 曾经用cocos2d-x写小游戏的时候就是这个非常难懂,或者能用,可是理解不多的话,非常难写出好的布局,难以适合商业化的应用. 游戏的布局有点像用photoshop画画的感觉.现有一个场景的概念,就像一个画布,然后上面分一层一层.能够单独某一层进行操作.显示的时候,能够觉得画面是从下往上一层一层堆上去.层里面有非常多精灵,精灵就是我们看到的那些会动的人物,建筑,怪什么的.这大概是cocos2d的设计思想吧. 我一直认为,

Android新手入门2016(13)--阻塞对话框PopupWindow

上两章都说了非阻塞的对话框,今天说一下阻塞的对话框--PopupWindow 那么什么是阻塞什么是非阻塞呢?PopupWindow和AlertDialog有什么不同呢? 先说AlertDialog,弹出来之后,背面会变灰,并没有阻塞后台的进程,如果没特殊控制,点击后面灰暗处,弹框会消失掉的. 至于PopupWindow,则是弹出来,后面没有任何变化,并且阻塞该应用的进程,如果一直没退出,应用汇一直等待,点击后面也是没有反应的. 不知道为什么现在上传不了图,就不上传了,其实跟AlertDialog

Android新手入门2016(15)--Gallery画廊

本文来自肥宝传说之路,引用必须注明出处! Gallery是Android查看图片的一个工具,用户使用非常方便. 可以通过左右滑动来查看不同的图片 代码比较简单,但是还是搞了一整天,因为碰到了一些问题. 主要是图片的来源问题,这里是通过Java的映射机制和R文件来获得drawable目录下的图片. 不过要注意,drawable类里面是有很多系统本身的属性,有些是不能显示出来的,所以要过滤,否则会报错. 另外放在drawable里面的图片,必须是png格式的.解决这两个问题就好办很多了. activ

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

本文来自肥宝传说之路,引用必须注明出处! ListView是Android中经常使用的控件. 什么是列表视图,让我们先看看图: 最常见的样例就是各种菜单的下啦列表. 要实现列表,须要完毕三个要素: 1.ListView 把全部的数据按指定的格式排成列表. 列表中每一项能够称为Item(如上图This is Title). 能够想象得出,要显示列表.就要先弄成相应的格式 2.adapter 适配器就是这样的ListView可以识别的格式,当然适配器有几种.以下再细说.适配器是指定格式的数据.可是我

Android新手入门2016(11)--非阻塞对话框AlertDialog

写了这么久,看了这么多控件,好像都是静态的,一点交互都没有.这次要弄点弹框,活跃活跃. 这次继续用上一章的代码往下面写吧. 先看看图 还是前一章的九宫图,我把对话框绑定在第一个图标. 点击一下,可以看到如下: 再来看看代码吧 package com.fable.helloworld; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android

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

本文来自肥宝传说之路.引用必须注明出处! GridView跟ListView一样是多控件布局.实现九宫图是最方便的. 还是先看看图,没图说个鸡鸡是不是 如上图.是一种应用方式.在每一个格子里面.放入应用图标,和显示应用的名字在下方. 以下先看看布局文件: activity_hello_world.xml <? xml version="1.0" encoding="utf-8"?> <GridView xmlns:android="htt

Android基础入门教程——10.14 Android GPS初涉

Android基础入门教程--10.14 Android GPS初涉 标签(空格分隔): Android基础入门教程 本节引言: 说到GPS这个名词,相信大家都不陌生,GPS全球定位技术嘛,嗯,Android中定位的方式 一般有这四种:GPS定位,WIFI定准,基站定位,AGPS定位(基站+GPS): 本系列教程只讲解GPS定位的基本使用!GPS是通过与卫星交互来获取设备当前的经纬度,准确 度较高,但也有一些缺点,最大的缺点就是:室内几乎无法使用-需要收到4颗卫星或以上 信号才能保证GPS的准确

Android基础入门教程——8.3.14 Paint几个枚举-常量值以及ShadowLayer阴影效果

Android基础入门教程--8.3.14 Paint几个枚举/常量值以及ShadowLayer阴影效果 标签(空格分隔): Android基础入门教程 本节引言: 在Android基础入门教程--8.3.1 三个绘图工具类详解Paint的方法参数那里我们就接触到 了这样几个东西:Paint.Style,Paint.Cap,Paint.Join等,这些都是Paint中的一些枚举值,相关 方法我们可以通过设置这些枚举值来设置特定效果比如:Style:画笔样式,Join图形结合方式等, 本节我们走进