导入的包是v4包要向下兼容3.0开始后才有新的fragment
在oncreatView的方法返回对应的view
public class myfragment extends Fragment {
@Override
public void onDetach() {
// TODO Auto-generated method stub
super.onDetach();
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.dd, container);
System.out.println("haha");
return inflate;
}
//在xml中配置fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment
android:name="com.example.fragment.myfragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>