Android: TableLayout trong Android

Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực

TableLayout trong Android sắp xếp nhóm các view vào trong các hàng và cột. Bạn sẽ sử dụng phần tử <TableRow> để xây dựng các hàng trong bảng. Mỗi hàng có 0 hoặc nhiều ô, mỗi ô có thể giữ một đối tượng View.

TableLayout Container không hiển thị các đường viền cho các hàng, cột hoặc ô của nó.

Table Layout trong Android

Các thuộc tính của TableLayout trong Android

Bảng dưới liệt kê một số thuộc tính riêng cho TableLayout:

Thuộc tính Miêu tả
android:id ID nhận diện TableLayout
android:collapseColumns Xác định chỉ mục của các cột (bắt đầu từ 0). Các chỉ mục cột được phân biệt riêng rẽ bằng dấu phảy: 0, 1, 2, ...
android:collapseColumns Chỉ mục (bắt đầu từ 0) của các cột để co lại. Các chỉ mục cột được phân biệt riêng rẽ bằng dấu phảy: 0, 1, 2, ...
android:stretchColumns Chỉ mục (bắt đầu từ 0) của các cột để kéo ra. Các chỉ mục cột được phân biệt riêng rẽ bằng dấu phảy: 0, 1, 2, 5

Ví dụ áp dụng

Ví dụ sau sẽ minh họa cách sử dụng TableLayout.

Sau đây là nội dung của file res/layout/activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity">

  <TableLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableRow
      android:layout_width="match_parent"
      android:layout_height="match_parent">

      <TextView
        android:id="@+id/txtView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/time"/>

      <TextClock android:id="@+id/txtClock"/>
    </TableRow>

    <TableRow
      android:layout_width="match_parent"
      android:layout_height="match_parent">

      <TextView
        android:id="@+id/txtFirstName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/first_name"/>

      <EditText
        android:id="@+id/firstName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"/>
    </TableRow>

    <TableRow
      android:layout_width="match_parent"
      android:layout_height="match_parent">

      <TextView
        android:id="@+id/txtLastName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/last_name"/>

      <EditText
        android:id="@+id/lastName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:autofillHints=""
        android:ems="10"
        android:inputType="textPersonName"/>
    </TableRow>

    <TableRow
      android:layout_width="match_parent"
      android:layout_height="match_parent">

      <TextView
        android:id="@+id/txtRatingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

      <RatingBar
        android:id="@+id/ratingBar"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

    </TableRow>

    <TableRow
      android:layout_width="match_parent"
      android:layout_height="match_parent">

      <TextView
        android:id="@+id/txtSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"/>

      <Button
        android:id="@+id/btnSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/submit"/>
    </TableRow>
  </TableLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

Sau đây là nội dung của res/values/strings.xml:

<resources>
  <string name="app_name">TableLayoutV1Study</string>
  <string name="time">Time:</string>
  <string name="first_name">First name:</string>
  <string name="last_name">Last name:</string>
  <string name="submit">Submit</string>
</resources>

Chạy ứng dụng ta được giao diện như sau:

Table Layout trong Android

» Tiếp: AbsoluteLayout trong Android
« Trước: RelativeLayout trong Android
Các khóa học qua video:
Python SQL Server PHP C# Lập trình C Java HTML5-CSS3-JavaScript
Học trên YouTube <76K/tháng. Đăng ký Hội viên
Viết nhanh hơn - Học tốt hơn
Giải phóng thời gian, khai phóng năng lực
Copied !!!