Lập trình Android - Frame Layout

FrameLayout trong Android

FrameLayout là một ViewGroup được sử dụng rất nhiều trong android. Bởi vì nó là ViewGroup đơn giản nhất, và thời gian tính toán của nó để layout ra các view con trong nó là thấp nhất nên performence của ViewGroup này là cao nhất.

FrameLayout được định nghĩa bắt đầu bởi thẻ <FrameLayout> và thẻ đóng </FrameLayout>. Ở giữa thẻ đóng và thẻ mở chính là các view con của nó.

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <!--View Child-->
 
</FrameLayout>

Quy tắc layout

Quy tắc layout các view con trong FrameLayout là các view sẽ nằng chồng lên nhau, view thêm vào sau sẽ nằm đè lên view nằm phía dưới. Ví dụ như sau:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/FrameLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="hiepsiit.com.MainActivity" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World"
        android:textColor="#2c3e50"
        android:textSize="32sp" />
 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hiep Si IT"
        android:textColor="#16a085"
        android:textSize="32sp" />

</FrameLayout>

Với đoạn mã trên thì "Hiep Si IT" sẽ nằm chồng lên "Hello World":

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/FrameLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="hiepsiit.com.MainActivity" >

   <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/avatar"/>
 
        <TextView
            android:layout_gravity="bottom|center"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Nguyễn Trương Minh Trí"
            android:textColor="#16a085"
            android:textSize="32sp" />
    </FrameLayout>
 

</FrameLayout>

Tên sẽ nằm trên hình ảnh

Ưu điểm

+ Là ViewGroup đơn giản nên thời gian tính toán để layout các view con nhanh.

Nhược điểm

+ Không thiết kế được cái giao diện phức tạp.


Download ví dụ