使用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 基本上写完了,让我们看看效果!

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

资源下载地址

使用FragmentTabhost代替Tabhost,布布扣,bubuko.com

时间: 2024-08-08 08:04:38

使用FragmentTabhost代替Tabhost的相关文章

使用FragmentTabhost取代Tabhost

   如今Fragment使用越来越广了,尽管Fragment寄生在Activity下.可是它的出现对于开发人员来说是一件很幸运的事,使开发的效率更高效了.好了以下就说说 FragmentTabhost的使用.由于Tabhost已经不推荐使用了,如今一般都使用FragmentTabhost! 我本身也个菜鸟,就是帮帮新手,由于Fragment是3.0才出现,为了避免3.0以下的使用不了,所以我们要用v4包来支持.不要倒错包哦.大神勿喷! 一:首先我们看看XML: 1.activity_main.

Android重写FragmentTabHost来实现状态保存

分类: android 2014-06-27 17:57 2077人阅读 评论(0) 收藏 举报 FragmentTabHost 最近要做一个类似QQ底部有气泡的功能,试了几个方案不太好,我想很多开发者使用TabHost都会知道它不保存状态,每次都要重新加载布局,为了保存状态,使用RadioGroup来实现,状态是可以保存了,问题是无法实现气泡功能,不能自定义布局,因为RadioGroup里面只能包含RadioButton,不然状态切换不起用作,这个可以查看RadioGroup源码,为了既能保存

[转]Android学习笔记:TabHost 和 FragmentTabHost

TabHost 命名空间: android.widget.TabHost 初始化函数(必须在addTab之前调用): setup(); 包含两个子元素: 1.Tab标签容器TabWidget(@android:id/tabs) 2.Tab内容容器FrameLayout(@android:id/tabcontent) FragmentTabHost 命名空间: android.support.v4.app.FragmentTabHost android.support.v13.app.Fragme

Android学习笔记:TabHost 和 FragmentTabHost

命名空间: android.widget.TabHost 初始化函数(必须在addTab之前调用): setup(); 包含两个子元素: 1.Tab标签容器TabWidget(@android:id/tabs) 2.Tab内容容器FrameLayout(@android:id/tabcontent) FragmentTabHost 命名空间: android.support.v4.app.FragmentTabHost android.support.v13.app.FragmentTabHos

FragmentActivity+FragmentTabHost+Fragement替代TabActibvity+TabHost+Activity

自Android3.2之后,TabActibvity被弃用(Deprecated).取而代之的是FragmentActivity.由于Fragment比Activiy更灵活.消耗的资源更小.全然可以满足TabActivity的效果,所以直接替代之.原来的TabActibvity+TabHost+Activity那套还可以用,只是强烈建议改用FragmentActivity+FragmentTabHost+Fragement FragmentTabHost使用方法: 1. 定义FragmentAc

安卓开发复习笔记——Fragment+FragmentTabHost组件(实现新浪微博底部菜单)

记得之前写过2篇关于底部菜单的实现,由于使用的是过时的TabHost类,虽然一样可以实现我们想要的效果,但作为学习,还是需要来了解下这个新引入类FragmentTabHost 之前2篇文章的链接: 安卓开发复习笔记——TabHost组件(一)(实现底部菜单导航) 安卓开发复习笔记——TabHost组件(二)(实现底部菜单导航) 关于Fragment类在之前的安卓开发复习笔记——Fragment+ViewPager组件(高仿微信界面)也介绍过,这里就不再重复阐述了. 国际惯例,先来张效果图: 下面

如何在Fragment中使用tabhost

最近在做一个仿电商的APP,由于前面使用了Fragment技术,现在想要在一个Fragment中做出TabHost的界面效果,经过查找资料找到了解决办法,特分享出来!(新人勿喷!) 首先要使用的控件是Support V4里面的控件,XML如图 <android.support.v4.app.FragmentTabHost         xmlns:android="http://schemas.android.com/apk/res/android"         andro

FragmentTabHost的使用方法总结

在开发商城上中时为了实现底部导航栏使用了fragmentTabHost方法,总结如下: 1.Activity必须继承fragmentActivity方法. 2.必须调用setup()方法 3.添加TabSpec 具体代码如下: 主要布局: activity_main: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.a

DrawerLayout,ToolBar 和 TabHost 的使用

ActionBar 定义起来不方便 toolbar: 最重要的特性,显示menu菜单,右上角三个点的菜单按钮,使用灵活 使用:1,布局文件,包裹LinearLayout 放imageView, 或者ImageButton 2,去除标题栏Action(清单文件中设置主题Theme.AppCompat.NoTitle) 3,setActionBar( toolbar ); Activity extends AppCompatActivity 4,创建menu文件夹下面的menu.xml文件 <?xm