一、创建一个activity(JingtaiActivity)
public class JingtaiActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jingtai);
}
}
二、创建一个Fragment(OneFragment)
注意创建fragment是需要去掉两个选项
代码区
public class OneFragment extends Fragment {
public OneFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
return view;
}
}
三、修改JingtaiActivity对应的布局文件activity_jingtai
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.administrator.myapplication.JingtaiActivity">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:textSize="35sp"
android:gravity="center"
android:text="这是activity界面"/>
<fragment
android:id="@+id/fragment_jingtai"
android:name="com.example.administrator.myapplication.fragment.OneFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
四、修改OneFragment对应的布局文件fragment_one
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.administrator.myapplication.fragment.OneFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:gravity="center"
android:text="fragment界面" />
</LinearLayout>
五、运行结果展现