zoukankan      html  css  js  c++  java
  • 【Android 开发教程】动态添加Fragments

    本章节翻译自《Beginning-Android-4-Application-Development》,如有翻译不当的地方,敬请指出。
    原书购买地址http://www.amazon.com/Beginning-Android-4-Application-Development/dp/1118199545/

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

    1. 新建工程。

    2. res/layout/main.xml

    [java] view plaincopy
     
    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="horizontal" >  
    6.   
    7. </LinearLayout>  
    3. res/layout/fragment1.xml
    [java] view plaincopy
     
    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:background="#00FF00"  
    6.     android:orientation="vertical" >  
    7.   
    8.     <TextView  
    9.         android:id="@+id/lblFragment1"  
    10.         android:layout_width="fill_parent"  
    11.         android:layout_height="wrap_content"  
    12.         android:text="This is fragment #1"  
    13.         android:textColor="#000000"  
    14.         android:textSize="25sp" />  
    15.   
    16. </LinearLayout>  
    4. res/layout/fragment2.xml
    [java] view plaincopy
     
    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:background="#FFFE00"  
    6.     android:orientation="vertical" >  
    7.   
    8.     <TextView  
    9.         android:layout_width="fill_parent"  
    10.         android:layout_height="wrap_content"  
    11.         android:text="This is fragment #2"  
    12.         android:textColor="#000000"  
    13.         android:textSize="25sp" />  
    14.   
    15. </LinearLayout>  
    5. Fragment1.java
    [java] view plaincopy
     
    1. public class Fragment1 extends Fragment {  
    2.     @Override  
    3.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
    4.             Bundle savedInstanceState) {  
    5.         // ---Inflate the layout for this fragment---  
    6.         return inflater.inflate(R.layout.fragment1, container, false);  
    7.     }  
    8. }  
    6. Fragment2.java
    [java] view plaincopy
     
    1. public class Fragment2 extends Fragment {  
    2.     @Override  
    3.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
    4.             Bundle savedInstanceState) {  
    5.         // ---Inflate the layout for this fragment---  
    6.         return inflater.inflate(R.layout.fragment2, container, false);  
    7.     }  
    8. }  

    7. FragmentsActivity.java

    [java] view plaincopy
     
    1. public class FragmentsActivity extends Activity {  
    2.     /** Called when the activity is first created. */  
    3.     @Override  
    4.     public void onCreate(Bundle savedInstanceState) {  
    5.         super.onCreate(savedInstanceState);  
    6.         setContentView(R.layout.main);  
    7.   
    8.         FragmentManager fragmentManager = getFragmentManager();  
    9.         FragmentTransaction fragmentTransaction = fragmentManager  
    10.                 .beginTransaction();  
    11.   
    12.         // ---get the current display info---  
    13.         WindowManager wm = getWindowManager();  
    14.         Display d = wm.getDefaultDisplay();  
    15.         if (d.getWidth() > d.getHeight()) {  
    16.             // ---landscape mode---  
    17.             Fragment1 fragment1 = new Fragment1();  
    18.             // android.R.id.content refers to the content  
    19.             // view of the activity  
    20.             fragmentTransaction.replace(android.R.id.content, fragment1);  
    21.         } else {  
    22.             // ---portrait mode---  
    23.             Fragment2 fragment2 = new Fragment2();  
    24.             fragmentTransaction.replace(android.R.id.content, fragment2);  
    25.         }  
    26.         // ---add to the back stack---  
    27.         fragmentTransaction.addToBackStack(null);  
    28.         fragmentTransaction.commit();  
    29.   
    30.     }  
    31.   
    32. }  

    8. 调试。

     
  • 相关阅读:
    【转载】关于sql server 代理(已禁用代理xp)
    Silverlight中将WriteableBitmap互转byte数组
    思迅Pay PC ,WIN7 ,KB3042058
    微软新Edge浏览器 WIN7 无法登录
    List<SelectListItem> 转为 SelectList
    图片jpg,png转为BASE64编码
    “Newtonsoft.Json”已拥有为“Microsoft.CSharp”定义的依赖项。
    mac os 10.15.1 懒人 .CDR
    微信刷脸SDK获取sub_openid
    win7 安装 visual studio 2019 时闪退(VS2019)
  • 原文地址:https://www.cnblogs.com/xgjblog/p/4618854.html
Copyright © 2011-2022 走看看