android中的2种fragment部署步骤

android中的2种fragment部署方法

1.静态类部署方法:

layout.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >


    <fragment
        android:id="@+id/fragment1"
        android:name="com.example.filedirtest.Fragment2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.20" />

</LinearLayout>

class:

import android.app.Fragment;    //注意这里导入的是此fragment

public static class Selffragment extends Fragment {
public Selffragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View root=inflater.inflate(R.layout.prefrence_fragment, container,false);
return root;
}
}

2.动态创建:

fragment类:

public class ExampleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootView=inflater.inflate(R.layout.fragment_layout, container, false);
return rootView;

}
}

activity中创建;

ExampleFragment fragment = new ExampleFragment();
getSupportFragmentManager().beginTransaction()
.add(R.id.fragmentContainer, fragment).commit();