`
zhanhao
  • 浏览: 286260 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

通过Compatibility Package低版本使用Fragment

 
阅读更多



 

android 3.0有很多新特性 其中有Fragment(碎片)通过这个可以实现View的模块化,特别在平板电脑中使用Fragment可以更加的灵活的布局,当我们想在3.0以下版本中使用Fragment !不过android提供了一种解决方式

---------Compatibility Package

Compatibility Package可以实现低版本的sdk使用高版本的一些特性

通过android-support-v4.jar 包实现(jar包位于android-sdk\extras\android\compatibility\v4下),在android-support-v4包中 有Fragment等类,在项目中build path一下android-support-v4.jar包!

使用:

在activity中引入Fragment有两种方式

1.xml:

 <fragment android:name="com.example.news.MyFragment1"

             android:id="@+id/viewer"   
             android:layout_weight="2"   
             android:layout_width="0dp"   
             android:layout_height="match_parent" />
 

2.代码:

 FragmentManager  fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.llFragmentList, new MyFragment3());
        fragmentTransaction.add(R.id.llFragmentDetail,new MyFragment4());
        fragmentTransaction.commit();

具体步骤:

1.新建一个类继承自Fragment

在Fragment中有个 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) ,这个方法就是的view就是显示在Activity中的view,

参数container指的是你要将Fragment添加到那个容器标签下

eg:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        LinearLayout layout = (LinearLayout)inflater.inflate(R.layout.myfragment1, container, false);
        Button btnAdd= (Button)layout.findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(this);
        Button btnRemove = (Button)layout.findViewById(R.id.btnRemove);
        btnRemove.setOnClickListener(this);
        Button btnReplace = (Button)layout.findViewById(R.id.btnReplace);
        btnReplace.setOnClickListener(this);
        return layout;
    }

2.新建一个类继承自FragmentActivity(必须继承自它)

然后通过

 FragmentManager  fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
        fragmentTransaction.add(R.id.llFragmentList, new MyFragment3());
        fragmentTransaction.add(R.id.llFragmentDetail,new MyFragment4());
        fragmentTransaction.commit();

的方式将Fragment添加到Activity中

Activity与Fragment通信的问题

Activity-->Fragment

通过fragmentManager.findFragmentById();或fragmentManager.findFragmentByTag()的方式查找到Fragment对象

Fragment-->Activity

在Fragment类中有一个 getActivity()方法,该方法获得的就是当前Fragment依赖的Activity实例

 

个人觉得Fragment类似ActivityGroup的效果,不过相对于ActivityGroup,Fragment提供了很多可以控制Fragment的方法

fragmentTransaction.add();

 fragmentTransaction.remove();

fragmentTransaction.replace();

transaction.addToBackStack(null); 等

更详细的关于Fragment的内容:

http://blog.sina.com.cn/s/blog_5d6ee3360100r1my.html

  • 大小: 31.7 KB
4
0
分享到:
评论
2 楼 ZHxin 2014-01-15  
在activity中引入Fragment的xml方式确信能在低版本中使用吗?
1 楼 down_data01 2012-06-27  
很有帮助,十分感激。。。

相关推荐

    Android Compatibility Package

    "Android Compatibility Package",通常被称为Android Support Library或AndroidX库,是Google为了确保应用程序在不同版本的Android系统上保持兼容性而开发的一系列库。这些库包含了多种组件和API,帮助开发者解决因...

    解决AD10不能导入低版本文件

    本文将详细介绍如何通过安装特定插件来解决AD10不能导入低版本文件的问题,并提供详细的步骤指导。 #### AD10简介 Altium Designer 10(简称AD10)是Altium公司推出的一款专业电路板设计软件,它集成了原理图设计...

    Application Compatibility Toolkit 5.5

    可以通过微软自己的Microsoft Application Compatibility Toolkit 5.5将信任程序加入系统白名单。使用方法: 1. 安装后得到三个程序,我们需要使用管理员权限启动Compatibility Administrator; 2. 在Custom ...

    Android 7.0 Compatibility List

    3.1. Managed API Compatibility 3.1.1. Android Extensions 3.2. Soft API Compatibility 3.2.1. Permissions 3.2.2. Build Parameters 3.2.3. Intent Compatibility 3.2.3.1. Core Application Intents 3.2.3.2. ...

    Electromagnetic Compatibility Principles and Applications.

    6. **设计策略**:在产品设计阶段就应考虑EMC,采用预兼容设计,包括选择低EMI元器件,优化布线,使用滤波器,以及合理布局以减少干扰路径。 7. **屏蔽技术**:金属屏蔽可以有效地减少电磁辐射和耦合,而电磁屏蔽...

    tensorflow gpu - TensorFlow, CUDA and cuDNN Compatibility - 兼容版本

    安装完成后,通过Python代码可以验证TensorFlow-GPU是否正确识别并使用了GPU设备。在TensorFlow 2.x中,可以使用`import tensorflow as tf; tf.test.gpu_device_name()`来检查,而在TensorFlow 1.x中,需要创建一个...

    A glibc compatibility layer package for Alpine Linux.zip

    Linux是一套免费使用和自由传播的类Unix操作系统,由林纳斯·托瓦兹于1991年首次发布。 Linux不仅是一个强大的操作系统,也是一个庞大的技术生态系统,涵盖了从服务器到个人电脑的各种应用场景。同时,它的开源特性...

    Browser compatibility issue

    2. **检测浏览器特性**:通过`navigator.userAgent`属性可以检测用户使用的浏览器类型和版本,然后根据检测结果决定是否执行特定代码。 3. **使用jQuery的跨浏览器API**:jQuery已经处理了很多跨浏览器的问题,比如...

    Microsoft.VisualBasic.Compatibility.rar

    Microsoft.VisualBasic.Compatibility Microsoft.VisualBasic.Compatibility Microsoft.VisualBasic.Compatibility Microsoft.VisualBasic.Compatibility Microsoft.VisualBasic.Compatibility ...各个版本

    Introduction to Electromagnetic Compatibility

    - **选择合适的材料和技术**:例如使用高性能的屏蔽材料、低噪声放大器等。 - **使用先进的仿真工具**:通过计算机辅助工程软件(如ANSYS HFSS、CST Microwave Studio等)进行模拟分析,预测潜在问题。 #### 五、...

    低版本兼容的ActionBar添加ActionProvider和实现分离式菜单

    这通常通过使用Android Support Library(现称为AndroidX库)和`compatibility package`来实现。例如,使用`android.support.v7.widget.Toolbar`替代原生的ActionBar,可以实现自定义样式和功能,并兼容到API级别7。...

    flink-hadoop-compatibility_2.11-1.10.0-API文档-中文版.zip

    赠送jar包:flink-hadoop-compatibility_2.11-1.10.0.jar; 赠送原API文档:flink-hadoop-compatibility_2.11-1.10.0-javadoc.jar; 赠送源代码:flink-hadoop-compatibility_2.11-1.10.0-sources.jar; 赠送Maven...

    低版本Office编辑Office2021文档的方法.docx

    标题和描述中提到的知识点是关于如何使用低版本的Office编辑高版本,特别是Office2021生成的文档。在本例中,问题的核心在于低版本...通过安装和使用该补丁,低版本的Office用户可以避免因文件格式不兼容而带来的不便。

    electromagnetic compatibility

    电磁兼容性(Electromagnetic Compatibility,简称EMC),是指设备或系统在其电磁环境中能正常工作且不对该环境中任何事物构成不能承受的电磁干扰的能力。在国际电工委员会(IEV161-01-07)的定义中,EMC被描述为...

    Electromagnetic Compatibility Engineering

    电磁兼容性(Electromagnetic Compatibility,简称EMC)是衡量电子设备在电磁环境中正常工作且不对该环境中其他设备造成不可承受的电磁干扰的能力。随着电子技术的发展与应用领域的不断扩展,EMC已经成为电子产品...

    如何解决word高版本制作的文档低版本打不开的问题.doc

    在使用Microsoft Word的过程中,我们时常会遇到一个棘手的问题:高版本Word制作的文档在低版本Word中无法正常打开。这种情况主要是由于不同版本之间存在格式差异,低版本的软件可能不支持高版本引入的新功能和格式。...

    ViewPager 案例

    首先通过SDK Manager更新最新版android compatibility package, revision 3 更新后,在eclipse中工程上点击右键,选择android tools -&gt; add compatibility library即可完成安装 实际上就是一个jar包,手工导到工程...

Global site tag (gtag.js) - Google Analytics