Original: Android 0 Basics 29th: Getting the most out of tablelayout table layouts
The linear layout and relative layout, while the linear layout is convenient, is difficult to meet if the control needs to be arranged neatly, and it is cumbersome to use relative layout, which provides table layout for this Android system.
I. Understanding Tablelayout
The table layout is to let the control arrange the controls in a tabular form, as long as the controls are placed in the cell, the controls can be neatly arranged, using the Tablelayout label.
Tablelayout inherits the LinearLayout, so its essence remains the linear layout manager. Each time you add a TableRow to Tablelayout, the TableRow is a table row, and TableRow is also a container, so it can also add additional components continuously, adding a column to the table for each child component. If you add a component directly to Tablelayout, the component will take up a row directly.
In a table layout, the width of the column is determined by the widest cell in the column, and the width of the entire table layout depends on the width of the parent container (the default is always full of the parent container itself).
In the table layout manager, you can set the following 3 ways to behave for cells.
Shrinkable: If a column is set to Shrinkable, the width of all cells in the column can be shrunk to ensure that the table adapts to the width of the parent container.
Stretchable: If a column is set to Stretchable, then the width of all the cells in the column can be stretched to ensure that the component fills the table free space completely.
Collapsed: If a column is set to collapsed, all cells of that column are hidden.
Tablelayout inherits the LinearLayout, so it can fully support all the XML attributes supported by LinearLayout. In addition to this, Tablelayout supports the XML attributes and related methods shown in the following table.
Ii. examples
Next, learn the usage of tablelayout using a simple example program.
Also using the Widgetsample project, continue to use the Activity_main.xml file in the app/main/res/layout/directory, where the following code snippet is populated:
<?XML version= "1.0" encoding= "Utf-8"?><LinearLayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent"android:orientation= "vertical"> <!--defines the first table layout, specifies that the 2nd column is allowed to shrink, and the 3rd column allows stretching - <TablelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:shrinkcolumns= "1"Android:stretchcolumns= "2"> <!--Add a button directly, it will take one line - <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "button on the line alone"/> <!--Add a table row - <TableRow> <!--add three buttons to the table row - <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Normal button"/> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "button to shrink"/> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "stretched button"/> </TableRow> </Tablelayout> <!--define the 2nd table layout, specify the 2nd column to hide - <TablelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:collapsecolumns= "1"> <!--Add a table row - <TableRow> <!--add three buttons to the table row - <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Normal button 1"/> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Normal button 2"/> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Normal button 3"/> </TableRow> </Tablelayout> <!--defines the 3rd table layout, specifying that the 2nd and 3rd columns can be stretched - <TablelayoutAndroid:layout_width= "Match_parent"Android:layout_height= "Wrap_content"Android:stretchcolumns= "Up"> <!--Define a table row - <TableRow> <!--add three buttons to the table row - <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Normal button" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "stretched button" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "stretched button" /> </TableRow> <!--Define a table row - <TableRow> <!--add two buttons to the table row - <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "Normal button" /> <ButtonAndroid:layout_width= "Wrap_content"Android:layout_height= "Wrap_content"Android:text= "stretched button" /> </TableRow> </Tablelayout></LinearLayout>
The bold code in the 3 tablelayout,3 tablelayout defined in the above page specifies their control behavior for each column.
1th Tablelayout, specifying that the 2nd column is allowed to shrink, and the 3rd column allows stretching.
2nd Tablelayout, specifying that the 2nd column is hidden.
3rd tablelayout, specifies that columns 2nd and 3rd allow stretching.
Running the program, you can see the interface effect shown.
Note that TableRow does not need to set width layout_width and height layoutjieight, its width must be match_parent, that is, automatically populate the parent container, the height must be wrap_content, that is, depending on the content to change the height. However, for other controls in TableRow, the width and height can be set, but must be wrap_content or fill_parent.
Here, Tablelayout's example ends, and more usage about tablelayout can be practiced in more hands.
Come here today, if you have any questions welcome message to discuss together, also welcome to join the Android 0 Basic introductory Technical discussion group, grow together!
This article copyright for the public Share talent show (Shareexpert)--Xin 鱻 all, if reproduced please note source, hereby declare!
Past period Summary share:
Android 0 Basics Introduction 1th: Android's past life
Android 0 Basics Section 2nd: Android system Architecture and application components those things
Android 0 Basics Section 3rd: Bring you up to talk about Android development environment
Android 0 Basics 4th: Installing and configuring the JDK correctly Ko fu the first trick
Android 0 Basics 5th: Use ADT bundles to easily meet the goddess
Android 0 Basics 6th: Configuration Optimization SDK Manager, official dating goddess
Android 0 Basics 7th: Take care of Android simulator and start the Sweet journey
Android 0 Basics 8th: HelloWorld, the starting point for my first trip
Android 0 Basics 9th: Android app, no code can be developed
Android 0 Basics Section 10th: Development IDE Big upgrade, finally ushered in Android Studio
Android 0 Basics Introductory Section 11th: Simple steps to take you to fly, run Android Studio project
Android 0 Basics 12th: Get familiar with the Android studio interface and start selling
Android 0 Basics 13th: Android Studio Configuration optimization to create a development tool
Android 0 Basics 14th: Using high-speed genymotion, stepping into the rocket era
Android 0 Basics Section 15th: Mastering the Android Studio project structure, sailing
Android 0 Basics Section 16th: Android User Interface Development overview
Android 0 Basics Section 17th: TextView Properties and Methods Daquan
Android 0 Basics Section 18th: EditText properties and how to use them
Android 0 Basics section 19th: Button usage explained
Android 0 Basics Section 20th: checkbox and RadioButton Usage Daquan
Android 0 Basics Section 21st: ToggleButton and switch usage Daquan
Android 0 Basics Section 22nd: ImageView's properties and methods Daquan
Android 0 Basics Section 23rd: ImageButton and Zoombutton use Daquan
Android 0 Basics Section 24th: Customize view simple to use to create your own controls
Android 0 Basics Section 25th: Simple and most commonly used linearlayout linear layouts
Android 0 Basics Section 26th: Two alignments, layout_gravity and gravity differ greatly
Android 0 Basics section 27th: Using padding and margin correctly
Android 0 Basics Section 28th: Easy to master relativelayout relative layout
Android 0 Basics 29th: Use tablelayout form layout