低版本兼容使用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进行设 ...
随机推荐
- 微信WeUI基础
首先引入样式css和js 虽然基础的其实还有weui.min.css,但是为了一些动态效果,也要引入其他的东西. 基本框架 <!DOCTYPE html> <html lang=&q ...
- SQL Serever学习12——数据库的备份和还原
公司的服务器奔溃了,事先没相应的保护措施,使得公司遭到了较大损失,为了以后不再出现类似事件,在系统中引入备份机制,使得数据库被破坏后损失降到最低. 数据的导出和导入 数据转换服务 数据转换服务DTS( ...
- SQL Serever学习7——数据表2
修改数据表的属性 保存数据表的修改的时候,总会弹出阻止修改的警告. 解决方法2种: 1.可以使用T-sql语句强行修改 2.工具>选项>设计器>表设计器和数据库设计器>阻止保存 ...
- Java - 接口还是抽象类
Java有两种机制可以为某个抽象提供多种实现——Interface和abstract class. Interface 和 abstract class, 除了比较明显的区别(也就是能否提供基本实现) ...
- java常见异常(转载)
版权声明: https://blog.csdn.net/qq_32595075/article/details/80059834 一般面试中java Exception(runtimeExceptio ...
- SecureCRT远程连接Linux下的sqlplus中退格键不能使用之解决方法
^H不是H键的意思,是backspace 主要是当你的终端backspace有问题的时候才需要设置 在linux环境下使用sqlplus,在回删(backspace)时往往会出现 一串的乱码.出现 ...
- InfluxDB 的卸载与重装
我是通过下面方式安装的,所以卸载也是用的 rpm 的卸载命令 wget http://s3.amazonaws.com/influxdb/influxdb-latest-1.x86_64.rpm su ...
- poj 2796 Feel Good 单调队列
Feel Good Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 8753 Accepted: 2367 Case Ti ...
- Vue指令的钩子函数使用方法
在Vue 中可以把一系列复杂的操作包装为一个指令. 什么是复杂的操作? 我的理解是:复杂逻辑功能的包装.违背数据驱动的 DOM 操作以及对一些 Hack 手段的掩盖等.我们总是期望以操作数据的形式来实 ...
- Javascript周报#182
This week’s JavaScript news Read this issue on the Web | Issue Archive JavaScript Weekly Issue 182Ma ...