CĂN BẢN
INTENTS
GIAO DIỆN
CÁC KHÁI NIỆM
VÍ DỤ
TÀI LIỆU
CÁC CHỦ ĐỀ
BÀI MỚI NHẤT
MỚI CẬP NHẬT

TableLayout trong Android

Trong bài này chúng ta sẽ tìm hiểu TalbeLayout trong Android, đây là một loại layout giúp bạn vẽ giao diện dưới dạng một table, tức là các view con ở trong sẽ được sắp xếp dưới dạng danh sách như một table.

test php

banquyen png
Bài viết này được đăng tại freetuts.net, không được copy dưới mọi hình thức.

Khi nói về UI Layout ta không thể không nói đến TableLayout. Ta thường sử dụng TableLayout để bố trí các view dưới dạng table, mỗi hàng là một TableRow, và bạn có thể thay đổi các thuộc tính để chỉnh giao diện cho TableRow đó. Vậy TableLayout trong Android là gì thì chúng ta cùng tìm hiểu ngay nhé.

1. TableLayout trong Android là gì?

Trong Android, TableLayout là một lớp con của ViewGroup được sử dụng để hiển thị các phần tử View con trong các hàng và cột.

Sau đây là hình ảnh minh họa của TableLayout trong ứng dụng Android.

Bài viết này được đăng tại [free tuts .net]

android table layout example diagram png

Trong Android, TableLayout sẽ định vị các phần tử con của nó thành các hàng và cột và nó sẽ không hiển thị đường viền nào cho các hàng, cột hoặc ô.

Mỗi hàng là một đối tượng view TableRow, bên trong TableRow chứa các View con, mỗi View con này nằm ở vị trí một ô bảng (cell).

Độ rộng của một cột được xác định mặc định bởi ô bảng có độ rộng lớn nhất ở các hàng tương ứng với cột, hoặc chúng ta có thể thiết lập.

TableLayout trong Android sẽ hoạt động giống như bảng HTML . Nếu ở HTML, TableLayout tương tự như phần tử <table> và TableRow tương tự như phần tử <tr>.

2. Ví dụ về TableLayout trong Android

Sau đây là ví dụ về việc tạo TableLayout trong ứng dụng Android.

Tạo một ứng dụng Android mới và đặt tên là TableLayout .

Bây giờ mở file activity_main.xml từ đường dẫn \ res \ layout và viết code như dưới đây

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="100dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp" >
    <TableRow android:background="#0079D6" android:padding="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="UserId" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="User Name" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Location" />
    </TableRow>
    <TableRow android:background="#DAE8FC" android:padding="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="1" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Suresh Dasari" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Hyderabad" />
    </TableRow>
    <TableRow android:background="#DAE8FC" android:padding="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="2" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Rohini Alavala" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Guntur" />
    </TableRow>
    <TableRow android:background="#DAE8FC" android:padding="5dp">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="3" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Trishika Dasari" />
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="Guntur" />
    </TableRow>
</TableLayout>

Khi tạo layout xong, chúng ta cần tải XML layout resource từ phương thức callback activity onCreate (). Mở file MainActivity.java từ đường dẫn \java\com.tutlane.tablelayout và viết code như như dưới đây

MainActivity.java

package com.tutlane.linearlayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

Theo code ở trên, chúng ta đang gọi layout bằng phương thức setContentView với tham số R.layout.layout_file_name. Ở đây, tên file xml là activity_main.xml, vì vậy chúng ta đã sử dụng R.layout.activity_main.

Nói chung, trong khi khởi chạy activity, phương thức callback onCreate () sẽ được gọi bởi framework của Android để lấy layout cần thiết cho activity đó.

3. Kết quả của ví dụ về TableLayout trong Android

Khi chạy ví dụ trên bằng thiết bị ảo Android (AVD), chúng ta sẽ nhận được kết quả như hiển thị bên dưới.

android table layout example output png

Đây là cách sử dụng table layout trong các ứng dụng Android dựa trên yêu cầu.

Cùng chuyên mục:

FrameLayout trong Android

FrameLayout trong Android

Trong các UI Layout, FrameLayout là loại Layout đơn giản nhất, xem ngay tại đây

Relative Layout trong Android

Relative Layout trong Android

Trong bài này chúng ta sẽ tìm hiểu RelativeLayout trong Android

LinearLayout trong Android

LinearLayout trong Android

Ở bài trước chúng ta đã tìm hiểu sơ qua về các loại layout trong…

UI Layout trong Android

UI Layout trong Android

Trong bài này chúng ta sẽ tìm hiểu về UI Layout trong Android

View và ViewGroup trong Android

View và ViewGroup trong Android

Các thành phần cơ bản để xây dựng giao diện người dùng (UI) trong Android…

Intent Filters trong Android

Intent Filters trong Android

Trong bài này chúng ta sẽ tìm hiểu về Intent Filter trong Android

Explicit Intents trong Android

Explicit Intents trong Android

Trong bài này chúng ta sẽ tìm hiểu một loại Intent mới trong Android đó…

Implicit Intents trong Android

Implicit Intents trong Android

Trong bài này chúng ta sẽ tìm hiểu Implicit Intent trong Android

Cấu trúc thư mục Project Android

Cấu trúc thư mục Project Android

Trong bài này chúng ta sẽ tìm hiểu cấu trúc thư mục của một project…

Giới thiệu Intent trong Android

Giới thiệu Intent trong Android

Trong Android, Intent là những tin nhắn không đồng bộ cho phép các component

Fragment trong Android

Fragment trong Android

Trong bài này chúng ta sẽ tìm hiểu một khái niệm mới trong lập trình…

Services trong Android

Services trong Android

Trong bài này chúng ta sẽ tìm hiểu Services trong Android.

Broadcast Receiver trong Android

Broadcast Receiver trong Android

Bài này sẽ tìm hiểu Broadcast Receiver trong Android.

Content Providers trong Android

Content Providers trong Android

Bài này chúng ta sẽ tìm hiểu về content provider trong Android, nó đóng vai…

Activity trong Android

Activity trong Android

Trong Android, Activity biểu thị một màn hình đơn với giao diện người dùng (UI)…

Component trong một ứng dụng Android

Component trong một ứng dụng Android

Tạo trình giả lập Android hoặc Tạo AVD (Thiết bị ảo Android)

Tạo trình giả lập Android hoặc Tạo AVD (Thiết bị ảo Android)

Thiết bị ảo Android (AVD) là một trình giả lập được sử dụng để sao…

Chương trình

Chương trình "Hello World" Android

Bằng cách sử dụng IDE Android Studio (Integrated Development Environment tạm dịch :Môi trường phát…

Cấu trúc hệ điều hành Android

Cấu trúc hệ điều hành Android

Trong bài này chúng ta sẽ tìm hiểu một chút về cấu trúc của hệ…

Cài đặt Android Studio từng bước có hình ảnh minh họa

Cài đặt Android Studio từng bước có hình ảnh minh họa

Trong bài này mình sẽ hướng dẫn các bạn cách download và cài đặt Android…

Top