Android Tab界面的实现-Fragment

  相信大家都用过qq,关于qq的界面是怎么实现的呢?其实它的界面就是我们今天要说的--用Fragment来实现tab。
  首先,我们来说说这个方式的优点:

   1.减少MainActivity类中的代码,将代码分配给相应的Fragment类中。
   2.由于创建了多个Fragment来管理布局,因此后期维护更加容易,只需要更改相应的Fragment就行。
   3.在单个Fragment中可以实现更多的功能,想一想qq的向右滑动与向左滑动。如果是viewpager,则不能这些功能。

  好,话不多说,直接贴代码

MainActivity布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.example.android_tab1.MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <include layout="@layout/top" />
    </LinearLayout>
    <FrameLayout 
        android:id="@+id/framelayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        ></FrameLayout>
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <include layout="@layout/bottom" />
    </LinearLayout>

</LinearLayout>

  其他的xml代码,比如top.xml, buttom.xml, view1.xml, view2.xml, view3.xml, view4.xml代码都与上一文章中的一样的,这里我就不贴了。

WeixinFragment代码
package com.example.Fragment;


import com.example.android_tab2.R;

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

public class WeixinFragment extends Fragment{
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        return inflater.inflate(R.layout.view1, container, false);
    }
}

  至于FriendFragment,AddressFragment,SettingsFragment的代码跟WeixinFragment的代码基本上是一样的,这里就不进行展示了。

MainActivity代码

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 176,034评论 25 709
  • 这是谷歌官方给我们提供的一个兼容低版本安卓设备的软件包,里面包囊了只有在安卓3.0以上可以使用的api。而view...
    Ten_Minutes阅读 11,030评论 1 19
  • 见若不见 已是许久未见 再见 不似从前 昔日的眸 不该似今日迷散 今日的笑 为何未见往昔的甘甜 我明了 那憔悴的脸...
    回忆是寂寞的香气阅读 1,027评论 0 1
  • 最近,一个工作过的朋友对我说了一句话,说我是工作中死的最快的一个人。我被这句话给打击到了,开始我还有点愤...
    雷TT阅读 2,972评论 0 0
  • FusionCharts是一个跨平台,跨浏览器的flash图表组件解决方案,能够被 ASP.NET, ASP, P...
    shallwego_阅读 7,040评论 1 0

友情链接更多精彩内容