Fragment的创建有以下2种常用方式
XML方式添加Fragment
Activity类中都不用动,在Activity的xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.gaby.test5.CommunicationActivity"
android:orientation="vertical">
<CheckBox
android:id="@+id/check_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="是否勾选"/>
<fragment
android:id="@+id/communication_fragment"
android:name="com.gaby.test5.CommunicationFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
CommunicationFragment类
public class CommunicationFragment extends Fragment {
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
private String mParam1;
private String mParam2;
public CommunicationFragment() {
}
public static CommunicationFragment newInstance(String param1, String param2) {
CommunicationFragment fragment = new CommunicationFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//将Fragment绑定的布局文件填充进来
return inflater.inflate(R.layout.fragment_communication, container, false);;
}
}
fragment_communication.xml
<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.gaby.test5.CommunicationFragment"
android:orientation="horizontal"
android:background="#000fff">
</LinearLayout>
Java代码方式添加Fragment
Activity所在的布局xml文件中将Fragment标签去除,添加一个FrameLayout占位
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
tools:context="com.gaby.test5.CommunicationActivity"
android:orientation="vertical">
<CheckBox
android:id="@+id/check_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="是否勾选"/>
<FrameLayout
android:id="@+id/communication_fragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Activity中如下关键代码
public class CommunicationActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_communication);
//创建Fragment
CommunicationFragment communicationFragment = new CommunicationFragment();
//java代码添加Fragment 写法1
FragmentManager fm = getSupportFragmentManager();
//声明事务
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.add(R.id.communication_fragment, communicationFragment)
fragmentTransaction.commit();
//java代码动态添加 链式写法 写法2
// getSupportFragmentManager().beginTransaction().add(R.id.communication_fragment, communicationFragment).commit();
}
}