低版本兼容使用Fragment (转)
Fragment框架开发东西确实很方便,但是恼人的是从4.0才开始支持。以前的版本必须用兼容模式开发,本人在网上找了大量资料,终于找到些线索正常运行于2.1版本的安卓系统。现在浅说一下兼容版本使用Fragment的基本要点。
1.首先libs目录必须有android-support-v4.jar,能用Fragment全靠它了。
2.使用的Activity必须继承自FragmentActivity。
3.不使用布局文件的<fragment />标签,使用其他layout作为容器,改用程序动态生成(笔者使用该标签始终报错)。动态生成的方法与4.0版本生成有所不同,具体见代码。
import android.os.Bundle;
// 必须引入android.support.v4.app
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View; /**
* 一定得继承自FragmentActivity
*
* @author Leon
* @version 2012
*/
public class MainActivity extends FragmentActivity
{
private int count = 0; // 测试用
FragmentManager fm; @Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iniFragment();
} /**
* 初始化fragment
*/
private void iniFragment()
{
fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.activity_main_fragment, new TestFragment());
ft.commit();
} /**
* 测试一下fragment
* @param v
*/
public void fragmentClick(View v)
{
TestFragment fragment = (TestFragment) fm.findFragmentById(R.id.activity_main_fragment);
fragment.setText("点击次数:" + ++count);
}
}
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView; public class TestFragment extends Fragment
{ @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.fragment_show, container, false);
} public void setText(String str)
{
((TextView) getActivity().findViewById(R.id.textView1)).setText(str);
}
}
布局文件activity_main.xml
<LinearLayout 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"
android:orientation="vertical" > <Button
android:id="@+id/activity_main_click_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello_world"
android:onClick="fragmentClick" />
<!-- 此LinearLayout用作fragment容器 -->
<LinearLayout
android:id="@+id/activity_main_fragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
fragment布局文件fragment_show.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" > <TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#007000" /> </LinearLayout>
低版本兼容使用Fragment (转)的更多相关文章
- Material Designer的低版本兼容实现(五)—— ActivityOptionsCompat
extends:http://www.cnblogs.com/tianzhijiexian/p/4087917.html 本文是对API中的方法做了介绍,如果想要看如何让这些方法兼容4.x或2.x可以 ...
- Material Designer的低版本兼容实现(一)—— 简介 & 目录
很长一段时间没写东西了,其实是因为最近在研究Material Designer这个东西,熬夜熬的身体也不是很好了.所以就偷懒没写东西,这回开的这个系列文章是讲如何将Material Designer在 ...
- fetch ios低版本兼容cannot clone a disturbed response
报错信息 ios 11以下 cannot clone a disturbed response github.com/github/fetc- 问题发生场景 使用了一个或者多个三方库 三方库或者自己的 ...
- IE低版本兼容的感悟
2017-04-09 曾经折磨一代人的兼容问题,如今也在同样折磨着我们,明明可以做JS判断来避免对ie低版本的兼容,但是却还是耐心的做着兼容,你可能会问这是为什么, 我们调的不是兼容,是整整一代人的情 ...
- Material Designer的低版本兼容实现(二)—— Theme
Theme material主题可以定义为如下形式: @android:style/Theme.Material @android:style/Theme.Material.Light @androi ...
- javascript原生bind方法ie低版本兼容详解
上一篇文章讲到了javascript原生的bind方法: http://www.cnblogs.com/liulangmao/p/3451669.html 这篇文章就在理解了原生bind方法的原理以后 ...
- Material Designer的低版本兼容实现(十)—— CheckBox & RadioButton
ChekBox的用途我们就不必多说了,算是一个很古老的控件了,何其类似的还有RadioButton,这个东西因为我目前还没写出来,所以用了别人的一个lib,这下面会说到.顺便说一句,如果你的app是在 ...
- Material Designer的低版本兼容实现(十二)—— Slider or SeekBar
Slider,我更喜欢叫他SeekBar,其实是一个东西啦,就是拖动条.5.0的拖动条和4.x上的HOLO风格完全不同,平添了一些精致.此外还加入了数值指示器,让用户在滑动的时候就能知道现在到了什么位 ...
- Material Designer的低版本兼容实现(十四)—— CardView
今天说的又是一个5.0中才有的新控件——CardView(卡片视图).这个东东其实我们早就见过了,无论是微博还是人人客户端,它都有出现.通常我们都是通过自定义一个背景图片,然后通过给layout进行设 ...
随机推荐
- 我的“MIT Challenge”
前言 在学习之余看到了一个有趣的挑战,名叫"MIT Challenge",这个挑战的目标是在一年365天之内学习 MIT 计算机系本科本科学生四年的课程.自己大二学习算法时也曾学习 ...
- angularjs 过滤掉textarea输入内容中夹带的特殊字符
<body ng-app="app"> <div ng-controller="main"> <textarea ng-model ...
- Xss和Csrf介绍
Xss和Csrf介绍 Xss Xss(跨站脚本攻击),全称Cross Site Scripting,恶意攻击者向web页面中植入恶意js代码,当用户浏览到该页时,植入的代码被执行,达到恶意攻击用户的目 ...
- Java基础——面向对象
Hello 大家好,我又来啦,今天我们来说说Java的面向对象. 还记得之前去面试几家公司的实习生职位,大部分面试官都问过我有关面向对象 的问题,不知道以后还会不会问,估计是不会了吧...(:3[▓▓ ...
- 获取java根目录,加载根目录下的文件
就两句代码 String filepath = System.getProperty("user.dir")+"/a.xlsx"; File file=new ...
- spss C# 二次开发 学习笔记(三)——Spss .Net 开发
Spss .Net 二次开发的学习过程暂停了一段时间,今天开始重启. 之前脑残的不得了,本想从网上下载一个Spss的安装包,然后安装学习.于是百度搜索Spss,在百度搜索框的列表中看到Spss17.S ...
- K:hash(哈希)碰撞攻击
相关介绍: 哈希表是一种查找效率极高的数据结构,很多语言都在内部实现了哈希表.理想情况下哈希表插入和查找操作的时间复杂度均为O(1),任何一个数据项可以在一个与哈希表长度无关的时间内计算出一个哈希值 ...
- js for循环中点击事件中无法获取每一个i值的问题
好像是第二次遇到这个问题,必须要总结一下!! <!DOCTYPE html> <html> <head> <meta charset="utf-8& ...
- Shellinabox安装及使用教程
本文转载自: shellinabox:一款使用 AJAX 的基于 Web 的终端模拟器 一.shellinabox简介 通常情况下,我们在访问任何远程服务器时,会使用常见的通信工具如OpenSSH和P ...
- C++笔记(1)----此运算符函数的参数太多
在VS2015中定义了这样一个类: #include<iostream> #include<vector> #include<string> using names ...