Can I use LinearLayout instead of FrameLayout?

I am creating an application with one Activity MainActivity , which consists of two fragments. And I came across many tutorials that use FrameLayout as the parent container for the fragment layout.

However, I found out that I can still use LinearLayout as the parent container of this fragment, and it works fine (for now).

My question is: is there any side effect of using FrameLayout as the parent container for the fragment layout?

If not, what is the advantage of using FrameLayout over LinearLayout (or another possible layout) as the parent container of the fragment layout.

+8
android android-layout android-linearlayout android-framelayout
source share
3 answers

Fragments themselves are displayed in the same way as all View , so you can use any parent ViewGroup that you want, depending on how you want your layout to look. This means that there is no advantage to LinearLayout vs FrameLayout tied to Fragments in general.

The main difference between FrameLayout and LinearLayout is that the Views stack is inside FrameLayout .

In other words, if you want your Fragments potentially overlap, use FrameLayout . If you want them displayed linearly, use LinearLayout . If it's a full-screen Fragment , then it probably doesn't matter what you choose.

+3
source share

FrameLayout is more efficient than LinearLayout and RelativeLayout. Because this is the simplest basic layout, and there is no connection between child layouts. FrameLayout is suitable for viewing the stack and is often used with ScrollView.

+4
source share

It's fine. You can use. But in general:

LinearLayout is used to align views one after the other vertically or horizontally. so that multiple child views can be ordered accordingly.

FrameLayout is used to load views one above the other. therefore, FrameLayout used to store one child view because it is difficult to manage multiple views in FrameLayout . For example. if you want to use a single CardView then FrameLayout perfect.

+2
source share

All Articles