低版本兼容使用Fragment

evade 2012-11-06 10:14:39
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>
...全文
594 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
ZHxinxin90 2014-01-15
  • 打赏
  • 举报
回复
赞同楼主,非常感谢!
Leo-Lian 2013-12-26
  • 打赏
  • 举报
回复
谢谢你的分享哦。
dazhong2012 2013-12-26
  • 打赏
  • 举报
回复
恩,我也纠结过,赞一个
卡卡的喵 2013-12-26
  • 打赏
  • 举报
回复
最近也在搞这个,多谢了
祥------- 2013-12-26
  • 打赏
  • 举报
回复
谢谢你的分享哦。

80,469

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧