簡體   English   中英

如何制作具有很多片段的應用程序

[英]How to make applications with many fragments

我想用很多片段和導航抽屜制作應用程序。 所以我用NavigationDrawerActivity開始了一個新項目,我的片段得到了很好的放置:

在此處輸入圖片說明

是否可以使用這個地方<include>來放置我的片段?

從 android studio 我收到了自動生成的代碼:

public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
}

我可以在這里創建一個邏輯,用我自己的片段替換自動生成的內容 main 嗎?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:baselineAligned="false"
    android:orientation="vertical">

    <FrameLayout
        android:id="@+id/frame1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1">
    </FrameLayout>

     <FrameLayout
         android:id="@+id/frame2"
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_weight="1" >
     </FrameLayout>

</LinearLayout>

另一種方式 :

FragmentTransaction ft = fragmentManager.beginTransaction();

Fragment frag1 = new Fragment();
ft.add(R.id.llfram, frag1, "fragment_1");

Fragment frag2 = new Fragment();
ft.add(R.id.llfram, frag2, "fragment_2");

ft.commit();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM