본문으로 건너뛰기

FrameLayout Tutorial and Examples

FrameLayout is a layout manager in Android that allows you to place child views on top of each other. The child views are arranged as frames, with the last added view appearing on top of all the others. In this article, we will explore the features of FrameLayout in Android and give code examples to demonstrate how it can be used.

Creating a FrameLayout

To create a FrameLayout, you need to add the following XML code to your layout file:

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_layout">
</FrameLayout>

This creates a FrameLayout with the ID frame_layout. You can add child views to this layout by adding more XML code inside the FrameLayout tags.

Adding Child Views

To add child views to the FrameLayout, you need to add the following code to your layout file:

<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/frame_layout">

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/image" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textSize="24sp"
android:layout_gravity="center" />

</FrameLayout>

This code adds an ImageView and a TextView to the FrameLayout. The ImageView takes up the entire layout and displays an image, while the TextView is centered in the layout and displays some text. Since the ImageView was added last, it appears on top of the TextView.

Modifying Child Views

You can modify the child views in a FrameLayout using their respective attributes. For example, to change the image displayed in the ImageView, you can add the following code:

<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/new_image" />

This changes the image displayed in the ImageView to a new image.

Using FrameLayout in Kotlin

To use FrameLayout in Kotlin, you can create a new instance of FrameLayout and add it to your layout using the following code:

val frameLayout = FrameLayout(this)
frameLayout.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
findViewById<ViewGroup>(R.id.layout_container).addView(frameLayout)

This code creates a new instance of FrameLayout, sets its layout parameters, and adds it to a container layout with the ID layout_container.

You can then add child views to the FrameLayout using the following code:

val imageView = ImageView(this)
imageView.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT)
imageView.setImageResource(R.drawable.image)
frameLayout.addView(imageView)

val textView = TextView(this)
textView.layoutParams = FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER)
textView.text = "Hello, World!"
textView.textSize = 24f
frameLayout.addView(textView)

This code creates new instances of ImageView and TextView, sets their layout parameters, and adds them to the FrameLayout.

Quick FrameLayout Examples

1. How to create a Square FrameLayout


import android.content.Context;
import android.util.AttributeSet;
import android.widget.FrameLayout;

public class SquareFramelayout extends FrameLayout {
public SquareFramelayout(Context context) {
super(context);
}

public SquareFramelayout(Context context, AttributeSet attrs) {
super(context, attrs);
}

public SquareFramelayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, widthMeasureSpec);
}
}

Conclusion

FrameLayout is a powerful layout manager in Android that allows you to place child views on top of each other. It is easy to use and allows for a wide range of customization options. By following the steps outlined in this article, you can easily create and modify FrameLayouts in your Android applications.