Chinaunix首页 | 论坛 | 博客
  • 博客访问: 450471
  • 博文数量: 145
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 1060
  • 用 户 组: 普通用户
  • 注册时间: 2013-08-22 11:52
个人简介

专注计算机技术: Linux Android 云计算 虚拟化 网络

文章分类

全部博文(145)

文章存档

2016年(3)

2015年(21)

2014年(75)

2013年(46)

我的朋友

分类: Android平台

2014-06-04 06:07:42

Fragment框架开发东西确实很方便,但是恼人的是从4.0才开始支持。以前的版本必须用兼容模式开发,本人在网上找了大量资料,终于找到些线索正常运行于2.1版本的安卓系统。现在浅说一下兼容版本使用Fragment的基本要点。

1.首先libs目录必须有android-support-v4.jar,能用Fragment全靠它了。

2.使用的Activity必须继承自FragmentActivity

3.不使用布局文件的标签,使用其他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


    xmlns:tools=""
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    
        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" />
        
             android:id="@+id/activity_main_fragment"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" />



fragment布局文件fragment_show.xml


    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    
        android:id="@+id/textView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="#007000" />

阅读(497) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~