Fragment的简单使用

MianActivity

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    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="finishactivity.zhangmeng.com.fragment.MainActivity">

    <fragment
        android:id="@+id/fragment_left"
        android:name="finishactivity.zhangmeng.com.fragment.LeftFragment"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"/>

    <fragment
        android:id="@+id/fragment_right"
        android:name="finishactivity.zhangmeng.com.fragment.RightFragment"
        android:layout_width="0dp"
        android:layout_weight="1"
        android:layout_height="match_parent"/>
</LinearLayout>

letfFragment

 1 package finishactivity.zhangmeng.com.fragment;
 2 
 3 import android.os.Bundle;
 4 import android.support.annotation.Nullable;
 5 import android.support.v4.app.Fragment;
 6 import android.view.LayoutInflater;
 7 import android.view.View;
 8 import android.view.ViewGroup;
 9 
10 /**
11  * Created by zhangmeng on 2016/9/28.
12  */
13 
14 public class LeftFragment extends Fragment {
15     @Nullable
16     @Override
17     public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
18         View view=inflater.inflate(R.layout.left_fragment,container,false);
19         return view;
20     }
21 }

leftFragment.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:orientation="vertical" android:layout_width="match_parent"
 4     android:layout_height="match_parent">
 5     <Button
 6         android:layout_width="wrap_content"
 7         android:layout_height="wrap_content"
 8         android:text="This is left fragment"/>
 9 
10 </LinearLayout>
View Code
原文地址:https://www.cnblogs.com/WebGiant/p/5917771.html