今天为向FragmentManager添加UI fragment以及添加字符串资源:
activity_crime.xml中
<?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragment_container" android:layout_width="match_parent" android:layout_height="match_parent"/>
CrimeActivity.java中
package com.bignerdranch.android.criminallntent; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v7.app.AppCompatActivity; public class CrimeActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_crime); android.app.FragmentManager fm=getFragmentManager(); Fragment fragment=fm.findFragmentById(R.id.fragment_container); if(fragment==null){ fragment=new CrimeFragment(); fm.beginTransaction().add(R.id.fragment_container,fragment).commit(); } } }