您的位置:首页 > 其它

低版本兼容使用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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: