【Android 开发教程】动态添加Fragments
来源:互联网 发布:台湾手机网络制式 编辑:程序博客网 时间:2024/05/19 13:08
本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/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>
- <?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>
- <?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>
- 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);
- }
- }
- 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
- public class FragmentsActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- 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. 调试。
- 【Android 开发教程】动态添加Fragments
- 【Android 开发教程】动态添加Fragments
- 【Android 开发教程】Fragments间的交互
- 动态添加Fragments
- 动态添加Fragments
- Android Fragments 官网教程
- 【Android 开发教程】添加Category
- 【Android 开发教程】添加Category
- android开发官方教程之Building a Dynamic UI with Fragments
- Android开发动态添加标签
- [Android--UI]用Fragments创建动态UI
- 碎片简介 —— 1 动态添加Fragments
- Android开发之Fragments之间的交互
- Android Fragments
- Android Fragments
- android fragments
- android--(Fragments)
- Android - Fragments
- 【Android 开发教程】使用Intent-Filter
- 【Android 开发教程】添加Category
- Hadoop中mapred包和mapreduce包的区别
- TortoiseSVN提交文件时'svn/!svn/me'path not found的解决
- C++统计字符串中(文件中)单词个数
- 【Android 开发教程】动态添加Fragments
- 解析网页源代码中的115网盘链接
- Hadoop动态添加删除datanode及tasktracker
- 【Android 开发教程】Fragment的生命周期
- MFC鼠标离开消息
- linux下用cron定时执行任务的方法
- java 实现伪静态地址
- Visual Studio 2010(.NET 4.0)中使用SQLite.NET
- Android保持屏幕常亮,PowerManager.WakeLock的使用