低版本兼容使用Fragment (转)

Fragment框架开发东西确实很方便,但是恼人的是从4.0才开始支持。以前的版本必须用兼容模式开发,本人在网上找了大量资料,终于找到些线索正常运行于2.1版本的安卓系统。现在浅说一下兼容版本使用Fragment的基本要点。
1.首先libs目录必须有android-support-v4.jar,能用Fragment全靠它了。
2.使用的Activity必须继承自FragmentActivity
3.不使用布局文件的<fragment />标签,使用其他layout作为容器,改用程序动态生成(笔者使用该标签始终报错)。动态生成的方法与4.0版本生成有所不同,具体见代码。

 1 import android.os.Bundle;
 2 // 必须引入android.support.v4.app
 3 import android.support.v4.app.FragmentActivity;
 4 import android.support.v4.app.FragmentManager;
 5 import android.support.v4.app.FragmentTransaction;
 6 import android.view.View;
 7 
 8 /**
 9  * 一定得继承自FragmentActivity
10  *
11  * @author Leon
12  * @version 2012
13  */
14 public class MainActivity extends FragmentActivity
15 {
16 private int count = 0; // 测试用
17 FragmentManager fm;
18 
19     @Override
20     public void onCreate(Bundle savedInstanceState)
21     {
22         super.onCreate(savedInstanceState);
23         setContentView(R.layout.activity_main);
24         iniFragment();
25     }
26 
27     /**
28      * 初始化fragment
29      */
30     private void iniFragment()
31     {
32         fm = getSupportFragmentManager();
33         FragmentTransaction ft = fm.beginTransaction();
34         ft.add(R.id.activity_main_fragment, new TestFragment());
35         ft.commit();
36     }
37     
38     /**
39      * 测试一下fragment
40      * @param v
41      */
42     public void fragmentClick(View v)
43     {
44         TestFragment fragment = (TestFragment) fm.findFragmentById(R.id.activity_main_fragment);
45         fragment.setText("点击次数:" + ++count);
46     }
47 }
 1 import android.os.Bundle;
 2 import android.support.v4.app.Fragment;
 3 import android.view.LayoutInflater;
 4 import android.view.View;
 5 import android.view.ViewGroup;
 6 import android.widget.TextView;
 7 
 8 
 9 public class TestFragment extends Fragment
10 {
11 
12 @Override
13 public View onCreateView(LayoutInflater inflater, ViewGroup container,
14 Bundle savedInstanceState)
15 {
16 return inflater.inflate(R.layout.fragment_show, container, false);
17 }
18 
19 public void setText(String str)
20 {
21 ((TextView) getActivity().findViewById(R.id.textView1)).setText(str);
22 }
23 }

布局文件activity_main.xml

 1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <Button
 8         android:id="@+id/activity_main_click_button"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:text="@string/hello_world"
12         android:onClick="fragmentClick" />
13         <!-- 此LinearLayout用作fragment容器 -->
14     <LinearLayout
15         android:id="@+id/activity_main_fragment"
16         android:layout_width="fill_parent"
17         android:layout_height="fill_parent" />
18 </LinearLayout>

fragment布局文件fragment_show.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="vertical" >
 6 
 7     <TextView
 8         android:id="@+id/textView1"
 9         android:layout_width="fill_parent"
10         android:layout_height="wrap_content"
11         android:background="#007000" />
12 
13 </LinearLayout>

 

 

posted on 2014-01-01 10:26  孤山独剑  阅读(302)  评论(0编辑  收藏  举报

导航