低版本兼容使用Fragment
2014-03-18 10:45
127 查看
Fragment框架开发东西确实很方便,但是恼人的是从4.0才开始支持。以前的版本必须用兼容模式开发,本人在网上找了大量资料,终于找到些线索正常运行于2.1版本的安卓系统。现在浅说一下兼容版本使用Fragment的基本要点。
1.首先libs目录必须有android-support-v4.jar,能用Fragment全靠它了。
2.使用的Activity必须继承自FragmentActivity。
3.不使用布局文件的<fragment />标签,使用其他layout作为容器,改用程序动态生成(笔者使用该标签始终报错)。动态生成的方法与4.0版本生成有所不同,具体见代码。
代码很简单,抛砖引玉,希望对苦手于fragment的人有所帮助^ ^
----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------
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>
1.首先libs目录必须有android-support-v4.jar,能用Fragment全靠它了。
2.使用的Activity必须继承自FragmentActivity。
3.不使用布局文件的<fragment />标签,使用其他layout作为容器,改用程序动态生成(笔者使用该标签始终报错)。动态生成的方法与4.0版本生成有所不同,具体见代码。
代码很简单,抛砖引玉,希望对苦手于fragment的人有所帮助^ ^
----------------------------------------------------------------------------------------------
----------------------------------------------------------------------------------------------
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>
相关文章推荐
- PHP环境搭建
- MyEclipse中防止代码格式化时出现换行的情况的设置
- POJ 3668 分数保存斜率
- Android 使用【AIDL】调用外部服务
- DevExpress之ASPxGridView笔记(1)
- 【javascript每日一练】- 无序数组排序
- 同wifi环境下android设备自动获取socket服务端的ip地址和端口号的方法-UDP广播
- (转)OPENCV 函数cvCreateMat
- SQLServer中的数据库备份恢复并重命名
- Mysql Explain 详解
- SSD1289驱动讲解
- myeclipse中将整块的代码所选中的代码左右移动的快捷键
- Codeforces Round #236 (Div. 2) D. Upgrading Array
- android style小结
- Kill Processes in Linux
- startActivityForResult
- (转)Java NIO框架Mina、Netty、Grizzly介绍与对比
- iframe布局 详细用法 及缺点
- 关于Java中split方法对空字符串处理问题
- Spring mvc (四)