Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it fine to use LinearLayout instead of FrameLayout?

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

However, I came to know that I can still use LinearLayout as parent container of the fragment and it works perfectly fine (so far).

My question, is there any side effect(s) of not using FrameLayout as the parent container for the fragment layout?

If there isn't, then what is the advantage of using FrameLayout over LinearLayout (or other possible layout) as the parent container of the fragment layout.

like image 705
fruqi Avatar asked Dec 31 '15 03:12

fruqi


People also ask

What's a FrameLayout When should you use FrameLayout instead of RelativeLayout?

A common rule of thumb when choosing layouts is to select the combination that results in the smallest number of nested layout views. Specific to your question, RelativeLayout is larger and more capable than the much simpler FrameLayout. So for simple layouts, the latter is probably more efficient.

Which has better performance LinearLayout or RelativeLayout?

So Linear Layout should preferred over Relative Layout! Also if you use weight_sum attribute within Linear Layout it would again “measure the child twice”. So avoid using this attribute as much as possible.

What is the difference between RelativeLayout and LinearLayout?

LinearLayout : is a ViewGroup that aligns all children in a single direction, vertically or horizontally. RelativeLayout : is a ViewGroup that displays child views in relative positions. AbsoluteLayout : allows us to specify the exact location of the child views and widgets.

Is ConstraintLayout faster than LinearLayout?

More complex layout but results are the same, flat Constraint Layout is slower than nested Linear Layout.


1 Answers

Fragments themselves are rendered just like any View, so you can use whatever parent ViewGroup you would like depending on how you want your layout to look. This means there's no inherent benefit of LinearLayout vs FrameLayout tied to Fragments at all.

The main difference between FrameLayout and LinearLayout is that Views stack within a FrameLayout.

In other words, if you want your Fragments to potentially overlap, use a FrameLayout. If you want them displayed linearly, use a LinearLayout. If it's a fullscreen Fragment then it likely doesn't matter which you choose.

like image 128
telkins Avatar answered Sep 21 '22 04:09

telkins