您好,登錄后才能下訂單哦!
Fragment的生命周期:
Fragment正常情況下從創建到銷毀的生命周期回調:onAttach(依附于宿主activity),onCreate(系統創建Fragment),onCreateView(創建布局文件),onActivityCreated(activity 的onCreate回調后會調用該生命周期方法),onStart(),onResume(),onPause(),onStop(),onDestroyView(),onDestroy(),onDetach()
旋轉屏幕時的生命周期:
2.?將手機屏幕向上,旋轉180度,不會觸發任何生命周期。
添加到Activity中:
<fragment
android:id="@+id/one_fragment"
android:name="com.cy.test.fragmentapplication.OneFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
ExampleFragment fragment = new ExampleFragment();
fragmentTransaction.add(R.id.fragment_container, fragment);//第一個參數是 ViewGroup,即應該放置片段的位置,由資源 ID 指定,第二個參數是要添加的片段。
fragmentTransaction.commit();
DialogFragment:
拓展DialogFragment需要實現onCreateView或者onCreateDialog:
// 實現onCreateView
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState) {
View inflate = inflater.inflate(R.layout.dialog_fragment_test1, container);
return inflate;
}
// 實現onCreateDialog
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
LayoutInflater inflater = Objects.requireNonNull(getActivity()).getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_fragment_test1, null));
return builder.create();
}
<!-- dialog_fragment_test1的布局文件 -->
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<TextView
android:id="@+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="50dp"
android:layout_marginTop="4dp"
android:text="測試。。。。巴拉巴拉巴拉巴拉巴拉。....巴拉。"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:id="@+id/guideline"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/guideline"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/guideline"
app:layout_constraintTop_toBottomOf="@+id/textView" />
</android.support.constraint.ConstraintLayout>
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。