灯火互联
管理员
管理员
  • 注册日期2011-07-27
  • 发帖数41778
  • QQ
  • 火币41290枚
  • 粉丝1086
  • 关注100
  • 终身成就奖
  • 最爱沙发
  • 忠实会员
  • 灌水天才奖
  • 贴图大师奖
  • 原创先锋奖
  • 特殊贡献奖
  • 宣传大使奖
  • 优秀斑竹奖
  • 社区明星
阅读:2894回复:0

Android 程式开发:(三)碎片简介 —— 3.1 动态添加

楼主#
更多 发布于:2012-09-06 14:04


fragment的真正用处是在程序运行过程中动态地添加。
1.工程目录。



2.res/layout/main.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="horizontal" >

</LinearLayout>
3.res/layout/fragment1.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:background="#00FF00"
    Android:orientation="vertical" >

    <TextView
        Android:id="@+id/lblFragment1"
        Android:layout_width="fill_parent"
        Android:layout_height="wrap_content"
        Android:text="This is fragment #1"
        Android:textColor="#000000"
        Android:textSize="25sp" />

</LinearLayout>
4.res/layout/fragment2.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:background="#FFFE00"
    Android:orientation="vertical" >

    <TextView
        Android:layout_width="fill_parent"
        Android:layout_height="wrap_content"
        Android:text="This is fragment #2"
        Android:textColor="#000000"
        Android:textSize="25sp" />

</LinearLayout>
5.Fragment1.java

package net.horsttnann.Fragments;

import net.horsttnann.Fragments.R;
import Android.app.Fragment;
import Android.os.Bundle;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.ViewGroup;

public class Fragment1 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // ---Inflate the layout for this fragment---
        return inflater.inflate(R.layout.fragment1, container, false);
    }
}
6.Fragment2.java

package net.horsttnann.Fragments;

import Android.app.Fragment;
import Android.os.Bundle;
import Android.view.LayoutInflater;
import Android.view.View;
import Android.view.ViewGroup;

public class Fragment2 extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // ---Inflate the layout for this fragment---
        return inflater.inflate(R.layout.fragment2, container, false);
    }
}
7.FragmentsActivity.java




package net.horsttnann.Fragments;

import net.horsttnann.Fragments.R;
import Android.app.Activity;
import Android.app.FragmentManager;
import Android.app.FragmentTransaction;
import Android.os.Bundle;
import Android.view.Display;
import Android.view.WindowManager;

public class FragmentsActivity extends Activity {
    /** Called when the activity is first created. */
    @Override  www.atcpu.com
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager
                .beginTransaction();

        // ---get the current display info---
        WindowManager wm = getWindowManager();
        Display d = wm.getDefaultDisplay();
        if (d.getWidth() > d.getHeight()) {
            // ---landscape mode---
            Fragment1 fragment1 = new Fragment1();
            // Android.R.id.content refers to the content
            // view of the activity
            fragmentTransaction.replace(Android.R.id.content, fragment1);
        } else {
            // ---portrait mode---
            Fragment2 fragment2 = new Fragment2();
            fragmentTransaction.replace(Android.R.id.content, fragment2);
        }
        // ---add to the back stack---
        fragmentTransaction.addToBackStack(null);
        fragmentTransaction.commit();

    }

}
8.调试。

效果图:





摘自 manoel的专栏

喜欢0 评分0
游客

返回顶部