android Fragment实现Tab功能(fragment相互切换时,可保存之前状态)

本文介绍了如何使用Fragment替代TabHost实现Tab功能,适用于Android开发初学者。通过实例讲解了如何在切换Fragment时保持之前的状态,并提供了FragmentActivity与Fragment间的数据传递方法。
摘要由CSDN通过智能技术生成

转载请标明出处:http://blog.csdn.net/jjsyjiao/article/details/40980909

由于TabHost已经逐渐过时,现在开发已经不建议使用,将由Fragment代替,本文主要讲述Fragment替换TabHost的方法,代码简单易懂,适合初学者使用。

上个效果图先:



首先:

1、建个资源文件activity_main.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" >


    <LinearLayout
        android:id="@+id/bottom"
        android:layout_width="match_parent"
        android:layout_height="60dip"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal" >


        <RelativeLayout
            android:id="@+id/fotune"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@color/bottom_background_onclick_color" >


            <ImageView
                android:id="@+id/fotune_img"
                android:layout_width="35dip"
                android:layout_height="35dip"
                android:layout_centerInParent="true"
                android:background="@drawable/fotune_a" />
        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/message"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@color/bottom_background_unclick_color" >


            <ImageView
                android:id="@+id/message_img"
                android:layout_width="35dip"
                android:layout_height="35dip"
                android:layout_centerInParent="true"
                android:background="@drawable/message_ia" />
        </RelativeLayout>


        <RelativeLayout
            android:id="@+id/account"
            android:layout_width="0dip"
            android:layout_height="fill_parent"
            android:layout_weight="1"
            android:background="@color/bottom_background_unclick_color" >


            <ImageView
                android:id="@+id/account_img"
                android:layout_width="35dip"
                android:layout_height="35dip"
                android:layout_centerInParent="true"
                android:bac
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值