Download Android FrameLayout Sample
FrameLayout is a simple layout to design user interface in Android applications. Frame layout can be used to design user interface if we have at least one of these 2 requirement...
In Frame layout, child views are drawn in a stack, with the most recently added child on top. The size of the FrameLayout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits). Views that are GONE are used for sizing only if setConsiderGoneChildrenWhenMeasuring() is set to true.
We can add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute.
Let's move on to our demo. In this example, an image is covering full width and height of screen and other images and text views are overlapping it. .
Layout file of above result is given below.
activity_main.xml
Here is some useful xml attributes of android frame layout
Xml attributes | Description |
---|---|
android:foreground | android:foreground points to the foreground that your Framelayout needs to draw on top of all its children |
android:foregroundGravity |
android:foregroundGravity points to the gravity of that asset, if that asset is a drawable. That means if you are using android:foreground="@android:drawable/bottom_bar"Then your drawable is drawn on top of all its children and it is positioned in the center of the Framelayout. |
android:measureAllChildren | android:measureAllChildren determines whether to measure all children or just those in the VISIBLE or INVISIBLE state when measuring. Defaults to false. That means, if it is set to false, then all the elements in the GONE state are not taken into consideration when the Framelayout's measure phase is going around. |
Here is another example of using android:foregroundGravity and
android:foregroundGravity
Layout file of above result is given below.
frame_layout.xml