简体   繁体   中英

How to programmatically add two Fragments to a layout?

i have trouble with attaching two fragments in one layout. I know how to add one fragment in a layout but I don't know how to put two fragments next to each other vertically in java android. here adding one fragment is fully explained but there is no tutorial for putting two or more fragments in one layout.

Create two FrameLayout in your xml file and place them the way you want. Add fragment in each of them.

Or see this link for nested fragments

You can try something like this:

FragmentTransaction ft = getFragmentManager()
                    .beginTransaction();
Fragment fragment = new MyAwsomeFragment();
            ft.add(R.id.linearLayoutActivity, fragment, null);//dont add to backstack
            ft.commit();

Here R.id.linearLayoutActivity points to a layout currently visible and inflated by the activity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM