2.2.3 TableLayout(表格布局)
Category Android Basic Tutorial
Introduction:
Previously, we have learned about LinearLayout and RelativeLayout, which are commonly used in actual development. In fact, mastering these two layouts is sufficient for most scenarios. The author personally uses these two layouts frequently in actual development. However, as an inquisitive programmer, it's always good to delve deeper into topics, even if they are not frequently used. You never know when they might come in handy! Right? Learning more doesn't hurt! Now, let's move on to this section where we will learn about Android's third layout: TableLayout!
1. Learning Roadmap
Roadmap Analysis: From the above roadmap, it's clear that the usage of TableLayout is quite straightforward. Essentially, it involves determining the number of rows and using three attributes to set the visibility, stretching, or shrinking of elements in specific columns.
2. Introduction to TableLayout
As those familiar with HTML know, we can create an HTML table using <table>
, <tr>
, and <td>
. Android also allows us to arrange components in a tabular format, i.e., in rows and columns, which is what TableLayout does. However, unlike the GridLayout introduced in Android 4.0, TableLayout does not allow direct setting of the number of rows and columns.
3. Determining the Number of Rows and Columns
- ① If we directly add components to TableLayout, each component will occupy a full row.
- ② To have multiple components in one row, we need to add a TableRow container and place the components inside it.
- ③ The number of components in a TableRow determines the number of columns in that row, and the width of each column is determined by the widest cell in that column.
- ④ The layout_width attribute of TableRow defaults to fill_parent, and setting it to other values won't take effect. However, layout_height defaults to wrap_content, and we can set its size.
- ⑤ The width of the entire TableLayout depends on the width of its parent container (it fills the parent container).
- ⑥ The number of rows is determined by the number of TableRows or standalone components. The number of columns is determined by the maximum number of components in any TableRow.
4. Three Common Attributes
android:collapseColumns: Sets the indices of columns to be hidden. android:shrinkColumns: Sets the indices of columns allowed to be shrunk. android:stretchColumns: Sets the indices of columns allowed to be stretched.
These attributes start counting columns from 0. For example, shrinkColumns = "2" refers to the third column. To set multiple columns, separate them with commas, e.g., "0,2". To apply to all columns, use the "*" symbol.
android:layoutcolumn ="2": Skips the second column and displays the component in the third column (counting from 1). android:layoutspan ="4": Merges four cells, meaning the component occupies four cells.
Attribute Usage Examples:
① collapseColumns (Hiding Columns)
Process: After defining 5 buttons in a TableRow, add the following attribute to the outer TableLayout: android:collapseColumns = "0,2" (hides the first and third columns).
<TableLayout
android:id="@+id/TableLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:collapseColumns="0,2" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="five" />
</TableRow>
</TableLayout>
② stretchColumns (Stretching Columns)
Process: After setting up four buttons in a TableLayout, add the following attribute to the outer TableLayout: android:stretchColumns = "1" (sets the second column to be stretchable).
<TableLayout
android:id="@+id/TableLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
</TableRow>
</TableLayout>
③ shrinkColumns (Shrinking Columns)
Process: To demonstrate the effect, set up five buttons and a text view. Add the following attribute to the outer TableLayout: android:shrinkColumns = "1" (sets the second column to be shrinkable).
<TableLayout
android:id="@+id/TableLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="1" >
<TableRow>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="one" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="two" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="three" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="four" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="five" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="文本XX" />
</TableRow>
</TableLayout>
5. Practical Example
Using TableLayout to create a simple login interface. The running effect is as follows:
Process Analysis:
① Use the gravity attribute, set to center_vertical, to center the components vertically within the layout. ② Set the first and fourth columns in TableLayout to be stretchable.
③ Add two TextView elements in each TableRow to stretch and fill the row, allowing the table to be horizontally centered.
Setting android:stretchColumns="0,3" to 0,3 is to fill both sides, so the middle part can be centered.
Detailed code is as follows:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/TableLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:stretchColumns="0,3"
android:gravity="center_vertical"
android:background="#66FF66"
>
<TableRow>
<TextView />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username:"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="150dp"/>
<TextView />
</TableRow>
<TableRow>
<TextView />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password:"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="150dp"
/>
<TextView />
</TableRow>
<TableRow>
<TextView />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Exit"/>
<TextView />
</TableRow>
</TableLayout>
6. Identified Issues
It's likely that you'll encounter a warning when using TableRow in TableLayout:
The program can still run, but if you're a perfectionist and the yellow warning sign bothers you, the solution is quite peculiar: simply ensure your TableLayout contains two or more TableRow elements!
Summary of This Section:
That concludes our discussion on Android's third layout: TableLayout. It's just about using five attributes, and we don't often use table layouts in actual development; knowing the basics is sufficient!
-1.0 Android Basic Beginner Tutorial
-1.0.1 2015 Latest Android Basic Beginner Tutorial Contents
-1.1 Background and System Architecture Analysis
-1.2 Development Environment Setup
-1.2.1 Developing Android APP with Eclipse + ADT + SDK
-1.2.2 Developing Android APP with Android Studio
-1.3 Solving SDK Update Issues
-1.4 Genymotion Emulator Installation
-1.5.1 Git Tutorial for Basic Local Repository Operations
-1.5.2 Git: Setting Up a Remote Repository on GitHub
-1.6 How to Use the 9-Patch Image
-1.7 Interface Prototype Design
-1.8 Project Source Analysis (Various Files, Resource Access)
-1.9 Signing and Packaging Android Applications
-1.11 Decompiling APK to Retrieve Code & Resources
-2.1 Concepts of View and ViewGroup
-2.2.1 LinearLayout (Linear Layout)
-2.2.2 RelativeLayout (Relative Layout)
- 2.2.3 TableLayout (Table Layout)
-2.2.4 FrameLayout (Frame Layout)
-2.2.5 GridLayout (Grid Layout)
-2.2.6 AbsoluteLayout (Absolute Layout)
-2.3.1 Detailed Explanation of TextView (Text Box)
-2.3.2 Detailed Explanation of EditText (Input Box)
-2.3.5.RadioButton (Radio Button) & Checkbox (Check Box)
-2.3.6 ToggleButton and Switch
-2.3.7 ProgressBar (Progress Bar)
-2.3.9 RatingBar (Star Rating Bar)
-2.4.1 ScrollView (Scroll Bar)
-2.4.2 Date & Time Components (Part 1)
-2.4.3 Date & Time Components (Part 2)
-2.4.4 Basic Explanation of Adapter
-2.4.5 Simple Usage of ListView
-2.4.6 Optimization of BaseAdapter
-2.4.7 Focus Issues with ListView
-2.4.8 Solving Checkbox Misalignment in ListView
-2.4.9 Data Update Issues with ListView
-2.5.0 Building a Reusable Custom BaseAdapter
-2.5.1 Implementing Multiple Item Layouts in ListView
-2.5.2 Basic Usage of GridView (Grid View)
-2.5.3 Basic Usage of Spinner (List Option Box)
-2.5.4 Basic Usage of AutoCompleteTextView (Auto-Complete Text Box)
- 2.5.5 Basic Usage of ExpandableListView (Collapsible List)
- 2.5.6 Basic Usage of ViewFlipper (Flip View)
- 2.5.7 Basic Usage of Toast
- 2.5.8 Detailed Explanation of Notification (Status Bar Notification)
- 2.5.9 Detailed Explanation of AlertDialog (Dialog Box)
- 2.6.0 Basic Usage of Other Common Dialogs
- 2.6.1 Basic Usage of PopupWindow (Floating Box)
- 2.6.2 Menu
- 2.6.3 Simple Usage of ViewPager
- 2.6.4 Simple Usage of DrawerLayout (Official Side Sliding Menu)
- 3.1.1 Event Handling Mechanism Based on Listeners
- 3.2 Event Handling Mechanism Based on Callbacks
- 3.3 Analysis of Handler Message Passing Mechanism
- 3.4 TouchListener vs OnTouchEvent + Multi-touch
- 3.5 Listening to Changes in EditText Content
- 3.6 Responding to System Setting Events (Configuration Class)
- 3.7 AsyncTask Asynchronous Task
- 3.8 Gestures
- 4.1.1 Introduction to Activity
- 4.1.2 Getting Started with Activity
- 4.1.3 Advanced Activity
- 4.2.1 Introduction to Service
- 4.2.2 Advanced Service
- 4.2.3 Mastering Service
- 4.3.1 Introduction to BroadcastReceiver
- 4.3.2 In-depth BroadcastReceiver
- 4.4.1 Introduction to ContentProvider
- 4.4.2 Further Exploration of ContentProvider – Document Provider
- 4.5.1 Basic Usage of Intent
- 4.5.2 Passing Complex Data with Intent
- 5.1 Basic Overview of Fragment
- 5.2.1 Fragment Example Analysis – Bottom Navigation Bar Implementation (Method 1)
- 5.2.2 Fragment Example Analysis – Bottom Navigation Bar Implementation (Method 2)
- 5.2.3 Fragment Example Analysis – Bottom Navigation Bar Implementation (Method 3)
- 5.2.4 Fragment Example Analysis – Bottom Navigation Bar + ViewPager Swipe to Switch Pages
- 5.2.5 Fragment Example Analysis – Simple Implementation of News (Shopping) App List Fragment
- 6.1 Data Storage and Access – File Storage and Reading
- 6.2 Data Storage and Access – SharedPreferences for Saving User Preferences
- 6.3.1 Data Storage and Access – Introduction to SQLite Database
- 6.3.2 Data Storage and Access – Further Exploration of SQLite Database
- 7.1.1 Android Network Programming Overview and HTTP Protocol Study
- 7.1.2 Study of Android HTTP Request Headers and Response Headers
- 7.1.3 Android HTTP Request Method: HttpURLConnection
- 7.1.4 Android HTTP Request Method: HttpClient
- 7.2.1 Android XML Data Parsing
- 7.2.2 Android JSON Data Parsing
- 7.3.1 Android File Upload
- 7.3.2 Android File Download (1)
- 7.3.3 Android File Download (2)
- 7.4 Android Calling WebService
- 7.5.1 Basic Usage of WebView (Web View)
- 7.5.2 Basic Interaction Between WebView and JavaScript
- 7.5.3 Considerations for WebView in Android 4.4 and Later
- 7.5.4 WebView File Download
- 7.5.5 WebView Cache Issues
- 7.5.6 WebView Handling Error Codes from Web Pages
- 7.6.1 Network Basics Preparation for Socket Study
- 7.6.2 Socket Communication Based on TCP Protocol (1)
- 7.6.3 Socket Communication Based on TCP Protocol (2)
- 7.6.4 Socket Communication Based on UDP Protocol
- 8.1.1 Summary of 13 Drawable Types in Android Part 1
- 8.1.2 Summary of 13 Drawable Types in Android Part 2
- 8.1.3 Summary of 13 Drawable Types in Android Part 3
- 8.2.1 Comprehensive Analysis of Bitmap (Bitmap) Part 1
- 8.2.2 OOM Issues Caused by Bitmap
- 8.3.1 Detailed Explanation of Three Drawing Tool Classes
- 8.3.2 Practical Examples of Drawing Classes
- 8.3.3 Paint API – MaskFilter (Mask)
- 8.3.4 Paint API – Xfermode and PorterDuff Detailed Explanation (Part 1)
- 8.3.5 Paint API – Xfermode and PorterDuff Detailed Explanation (Part 2)
- 8.3.6 Paint API – Xfermode and PorterDuff Detailed Explanation (Part 3)
- 8.3.7 Paint API – Xfermode and PorterDuff Detailed Explanation (Part 4)
- 8.3.8 Paint API – Xfermode and PorterDuff Detailed Explanation (Part 5)
- 8.3.9 Paint API – ColorFilter (Color Filter) (1/3)
- 8.3.10 Paint API – ColorFilter (Color Filter) (2/3) -8.3.11 Paint API - ColorFilter (Color Filter) (3-3)
-8.3.12 Paint API - PathEffect (Path Effect)
-8.3.13 Paint API - Shader (Image Rendering)
-8.3.14 Paint Enumerations/Constants and ShadowLayer Shadow Effect
-8.3.15 Paint API - Typeface (Font Style)
-8.3.16 Canvas API Detailed (Part 1)
-8.3.17 Canvas API Detailed (Part 2) Clipping Methods Collection
-8.3.18 Canvas API Detailed (Part 3) Matrix and drawBitmapMesh
-8.4.1 Android Animation Collection - Frame Animation
-8.4.2 Android Animation Collection - Tween Animation
-8.4.3 Android Animation Collection - Property Animation - First Encounter
-8.4.4 Android Animation Collection - Property Animation - Second Encounter
-9.1 Using SoundPool to Play Sound Effects (Duang~)
-9.2 MediaPlayer to Play Audio and Video
-9.3 Using Camera to Take Photos
-9.4 Using MediaRecord to Record Audio
-10.1 TelephonyManager (Phone Manager)
-10.2 SmsManager (SMS Manager)
-10.3 AudioManager (Audio Manager)
-10.5 AlarmManager (Alarm Service)
-10.6 PowerManager (Power Service)
-10.7 WindowManager (Window Management Service)
-10.8 LayoutInflater (Layout Service)
-10.9 WallpaperManager (Wallpaper Manager)
-10.10 Sensor Topic (1) - Introduction
-10.11 Sensor Topic (2) - Orientation Sensor
-10.12 Sensor Topic (3) - Accelerometer/Gyroscope Sensor
-10.12 Sensor Topic (4) - Understanding Other Sensors
-10.14 Android GPS Introduction
-11.0《2015最新Android基础入门教程》Completion Celebration~
-12.2 DrySister Look at Girls App (Version 1) - 2. Parsing Backend Data
-12.4 DrySister Look at Girls App (Version 1) - 4. Adding Data Caching (Introducing SQLite)
-12.5 DrySister Look at Girls App (Version 1) - 5. Code Review, Adjustment, and Logging Class Writing