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();
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/264509.html
標籤:其他
上一篇:安卓intent求助!!
