Android 使用FragmentTabhost代替Tabhost

Android 使用FragmentTabhost代替Tabhost

前言:

现在Fragment使用越来越广了,虽然Fragment寄生在Activity下,但是它的出现对于开发者来说是一件非常幸运的事,使开发的效率更高效了,好了下面就说说 FragmentTabhost的使用,因为Tabhost已经不推荐使用了,现在一般都使用FragmentTabhost!我本身也个菜鸟,就是帮帮新手,因为Fragment是3.0才出现,为了避免3.0以下的使用不了,所以我们要用v4包来支持,不要倒错包哦!大神勿喷!

一:首先我们看看XML:

1.activity_main.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/bg_tabhost_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_item_view.xml

<?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.fragment1.xml 就贴一个Fragment XML吧!其他的几个都一样,只是颜色不一样,呵呵!

<?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="#FBB55D" > 

</LinearLayout>

ok,XML先写完了,那我们看看代码吧!

4.MainActivity

package com.example.fragmenttabhost; 

import android.os.Bundle;
import android.support.v4.app.Fragment;
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.example.fragment.Fragment1;
import com.example.fragment.Fragment2;
import com.example.fragment.Fragment3;
import com.example.fragment.Fragment4;
import com.example.fragment.Fragment5; 

/**
 *
 * @author zqy
 *
 */
public class MainActivity extends FragmentActivity {
 /**
  * FragmentTabhost
  */
 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.tab_home_btn,
   R.drawable.tab_message_btn, R.drawable.tab_selfinfo_btn,
   R.drawable.tab_square_btn, R.drawable.tab_more_btn }; 

 /**
  * 选修卡文字
  *
  */
 private String mTextArray[] = { "首页", "消息", "好友", "搜索", "更多" };
 /**
  *
  *
  */
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main); 

  initView();
 } 

 /**
  * 初始化组件
  */
 private void initView() {
  mLayoutInflater = LayoutInflater.from(this); 

  // 找到TabHost
  mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
  mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
  // 得到fragment的个数
  int count = mFragmentArray.length;
  for (int i = 0; i < count; i++) {
   // 给每个Tab按钮设置图标、文字和内容
   TabSpec tabSpec = mTabHost.newTabSpec(mTextArray[i])
     .setIndicator(getTabItemView(i));
   // 将Tab按钮添加进Tab选项卡中
   mTabHost.addTab(tabSpec, mFragmentArray[i], null);
   // 设置Tab按钮的背景
   mTabHost.getTabWidget().getChildAt(i)
     .setBackgroundResource(R.drawable.selector_tab_background);
  }
 } 

 /**
  *
  * 给每个Tab按钮设置图标和文字
  */
 private View getTabItemView(int index) {
  View view = mLayoutInflater.inflate(R.layout.tab_item_view, null);
  ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
  imageView.setImageResource(mImageArray[index]);
  TextView textView = (TextView) view.findViewById(R.id.textview);
  textView.setText(mTextArray[index]); 

  return view;
 } 

}

5.Fragment1.java  Fragment其他几个都一样,指不过XML不一样!

package com.example.fragment; 

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; 

import com.example.fragmenttabhost.R; 

public class Fragment1 extends Fragment{ 

 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container,
   Bundle savedInstanceState) { 

  return inflater.inflate(R.layout.fragment1, null);
 }
}

OK 基本上写完了,让我们看看效果!

哈哈,效果还算可以!好了,去吃饭了!

资源下载地址:http://xiazai.jb51.net/201705/yuanma/FragmentTabhost(jb51.net).rar

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

(0)

相关推荐

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

    采取的方法是Fragment+FragmentTabHost组件来实现这种常见的app主页面的效果 首先给出main.xml文件 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical"

  • FragmentTabHost使用方法详解

    FragmentTabHost是support-v包下提供的用于集成和管理Fragment页面的组件. 今天要实现的效果图如下: 整体结构是MainActivity+5个模块的Fragment. MainActivity的布局如下: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/a

  • FragmentTabHost FrameLayout实现底部导航栏

    app经常用到底部导航栏,早前使用过RadioGroup+FrameLayout实现或者RadioGroup+ViewPager实现,现在基本使用FragmentTabHost+FrameLayout来实现,因为使用起来简单易用.下面写一个小例子简要地总结一下这个组合. 首先,看一下例子的最终运行效果图 这5个图标的效果其实都是一样的,只要做出来一个,以此类推就可以写出其他几个 第一步, FragmentTabHost+FrameLayout布局,先看一下代码: <?xml version=&quo

  • Android 使用FragmentTabhost代替Tabhost

    Android 使用FragmentTabhost代替Tabhost 前言: 现在Fragment使用越来越广了,虽然Fragment寄生在Activity下,但是它的出现对于开发者来说是一件非常幸运的事,使开发的效率更高效了,好了下面就说说 FragmentTabhost的使用,因为Tabhost已经不推荐使用了,现在一般都使用FragmentTabhost!我本身也个菜鸟,就是帮帮新手,因为Fragment是3.0才出现,为了避免3.0以下的使用不了,所以我们要用v4包来支持,不要倒错包哦!

  • Android 使用FragmentTabHost实现底部菜单功能

    前言 现在大部分App底部都有一个菜单,实现这个功能也有好多办法: - TabHost+Fragment - RadioGroup+Fragment - FragmentTabHost+Fragment - 5.0以后有个新控件,BottomNavigator 这篇主要介绍下FragmentTabHost配合Fragment使用 运行效果图 效果图分析 FragmentTabHost简单介绍 首先我们看下官方文档的介绍 OK,接着官方还给出了一份使用的代码,我们也来看下 Activity中使用

  • Android控件之TabHost用法实例分析

    本文实例讲述了Android控件之TabHost用法.分享给大家供大家参考.具体如下: 以下通过TabHost实现android选项卡. main.xml布局文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width=

  • Android编程实现设置TabHost当中字体的方法

    本文实例讲述了Android编程设置TabHost当中字体的方法.分享给大家供大家参考,具体如下: TabWidget tw=this.getTabWidget();//设置TabHost当中的内容的方法 for(int i=0;i<tw.getChildCount();i++){ //设置TabHost字体的相关方法 TextView tv=(TextView)tw.getChildAt(i).findViewById(android.R.id.title); tv.setGravity(BI

  • Android应用中使用TabHost组件继承TabActivity的布局方法

    继承TabActivity并以activity布局 先查看下最终效果图: 再看下代码结构: 其中black.gif顾名思义就是一个黑背景图片,grey.gif就是一张灰色的背景图片 然后直接上代码: ArtistActivity.java package cn.com.tagview; import android.app.Activity; import android.os.Bundle; import android.widget.TextView; public class Artist

  • 详解Android应用中使用TabHost组件进行布局的基本方法

    TabHost布局文件 我们先来了解一下布局文件的基本内容: 1. 根标签及id 设置Android自带id : XML布局文件中, 可以使用 标签设置, 其中的id 需要引用 android的自带id : android:id=@android:id/tabhost ; getHost()获取前提 : 设置了该id之后, 在Activity界面可以使用 getHost(), 获取这个TabHost 视图对象; 示例 : 复制代码 代码如下: <tabhost android:id="@a

  • 实例讲解Android应用开发中TabHost的使用要点

    Tab与TabHost: 这就是Tab,而盛放Tab的容器就是TabHost . 如何实现?? 每一个Tab还对应了一个布局,这个就有点好玩了.一个Activity,对应了多个功能布局. 新建一个Tab项目,注意,不要生成main Activity . 注意IDE,这里不要选... 在包里面新建一个类MyTab,继承于TabActivity. 其实,TabActivity是Activity的子类 package zyf.tab.test; import android.app.TabActivi

  • android 选项卡(TabHost)如何放置在屏幕的底部

    今天写Tab的时候由于TAB的跳转问题去查资料,倒反而发现更有趣的问题,就是如何将TAB放置在屏幕的底端. 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android" android:id="@android:id/tabhost" a

  • android TabHost(选项卡)的使用方法

    首先,定义TabHost的布局文件: 复制代码 代码如下: <?xml version="1.0" encoding="utf-8"?> <TabHost xmlns:android="http://schemas.android.com/apk/res/android"    android:id="@android:id/tabhost" android:layout_width="fill_p

随机推荐