내 Android 앱에는 특정 흐름에 7 개의 화면이있는 것이 요구됩니다. 이제 각각의 화면에는 공통적 인 상단과 하단이 있습니다. 그래서 FragmentActivity
과 7 개의 다른 Fragments
을 만들도록 선택했습니다. 런타임시 조각을 FragmentActivity
에 어떻게 삽입합니까? 나는이 튜토리얼 here 읽고,이 튜토리얼에 따라 한 내 주요 FragmentActivity
다음과 같은 레이아웃이 있어야합니다조각 레이아웃 사용
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<FrameLayout
android:id="@+id/fragment_content"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
을 그리고 조각 교체하려면 다음 코드를 사용한다 :
FragmentManager fm = getSupportFragmentManager();
Fragment fragment = fm.findFragmentById(R.id.fragment_content);
if (fragment == null) {
FragmentTransaction ft = fm.beginTransaction();
ft.add(R.id.fragment_content, new BasicFragment());
ft.commit();
}
내가 돈을 이것은 삽입됩니다,
ft.add(R.id.fragment_content, new BasicFragment());
R.id.fragment_content
가 FrameLayout
입니다 : t 이해 '는 다음 줄입니다 조각으로 FrameLayout
또는 무엇?
IT는'Fragment'를 식별하기 위해'tag'를 사용한다. 'Fragment'가 xml에있는 경우'id'를 사용합니다. 여기서는 그렇지 않습니다. 프로그래밍 방식으로 추가하는 것입니다 (더 나은 방법). –