动态添加Fragments

原址:http://blog.csdn.net/manoel/article/details/7577349

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>  

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> 

 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>  

Fragment1.java

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

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();  
  
    }  
  
}  

上述代码在手机横屏、竖屏事将加载不同内容

代码下载:http://download.csdn.net/detail/xjntosc/5308421


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值