Android user interface design: Table layout

Source: Internet
Author: User
Keywords Layout can XML we

A table layout chart can be used to display tabular data or to make a neat interface like an HTML table on a Web page. This tutorial describes how to use the XML layout file separately and create a table layout using code.

Understanding layouts is important for good Android programming. In this tutorial, you will learn about the framework layout, which is primarily used to organize user interface controls or gadgets in neat rows and columns on the interface. With proper use, tabular layouts can be powerful examples of how Android programs can design their interfaces and display tabular data.

&http://www.aliyun.com/zixun/aggregation/37954.html ">nbsp;

What is a table layout?

As it literally means, a table layout is a grid of rows and columns, and you can display view controls in cells in those grids. From the perspective of user interface design, a tablelayout consists of a series of TableRow controls, each of which corresponds to a row in the table for each TableRow control. The contents of the TableRow are composed of the view controls in the cell.

The appearance of Tablelayout is managed by some additional rules. First, the number of columns in the entire table is the same as the number of columns in the table. Second, the column widths are defined as the widths of the columns that display the maximum width of the content. The Layout_width property of the Tablelayout Zikong and subunits is always set to match_parent--although they can be defined in the XML file, their actual width cannot be overwritten. The layout_height of tablelayout cells can also be defined, but TableRow Layout_height property values are always wrap_content. Cells can span columns, but not across rows. This feature can be implemented through the Layout_span properties of TableRow's child views. A cell is a single child view in a TableRow. If you want a more complex cell with multiple views, you need to encapsulate other views with a layout view.

In other words, some rules can be modified. A column can be identified as stretched, which means its width can be extended to the width of the parent container. Columns can also be identified as compressible, which means that the width can be narrowed so that the entire row conforms to the space provided by the parent container. You can also combine a whole column.

If you want to see the complete documentation for the table layout, visit the Android SDK documentation for the Tablelayout class. The related XML attributes used in XML resources are also defined in the literature.

design a simple table layout

The layout is explained most convincingly by an instance, and the table layout is no exception. Let's say we want to design an interface to show the weather in the next few days. Organizing information with a table layout is a good choice:

in the first TableRow, we can display the title of the interface. In the second TableRow, we can display the date in a calendar-like format. In the third TableRow, we can display high temperature information on a daily basis. In the fourth TableRow, we can display the daily low temperature information. In the fifth TableRow, we can show some patterns that describe the weather conditions, such as rainy days, snow, sunny, or cloudy.

The first image shows the preview screen in the layout editor:

uses XML layout resources to define table layouts

The most convenient and maintainable way to design a program's user interface is to create an XML layout resource. This approach greatly simplifies the UI design process by moving many static creation and user interface control layouts and the definition of control properties to XML, instead of writing code.

The XML layout resource must be stored in the/res/layout project directory. Let's take a look at the table layout described in the previous section. This layout resource file is named/res/layout/framed.xml and is defined in XML as follows:

<?xml version= "1.0" encoding= "Utf-8" ><tablelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "android:id=" @+id/tablelayout1 "android:layout_width=" match_parent "android:layout_height=" Match_parent " android:shrinkcolumns= "*" android:stretchcolumns= "*" > <tablerow android:id= "@+id/tablerow4" Android:layout_ height= "Wrap_content" android:layout_width= "match_parent" android:gravity= "Center_horizontal" > <TextView Android:id= "@+id/textview9" android:layout_width= "match_parent" android:layout_height= "Wrap_content" Android: Textstyle= "Bold" android:typeface= "serif" android:textsize= "18DP" android:text= "Weather Table" android:gravity= " Center "android:layout_span=" 6 "></TextView> </TableRow> <tablerow android:id=" @+id/tablerow1 " android:layout_height= "Wrap_content" android:layout_width= "match_parent" > <textview android:id= "@+id/ TextView04 "android:text=" "></TextView> <textview android:id=" @+id/textview04 "android:text=" Modified 7 "aNdroid:textstyle= "Bold" android:typeface= "serif" ></TextView> <textview android:id= "@+id/textview03" android:text= "Modified 8" android:textstyle= "bold" android:typeface= "serif" ></TextView> <textview android:id = "@+id/textview02" android:text= "Modified 9" android:textstyle= "bold" android:typeface= "serif" ></TextView> < TextView android:id= "@+id/textview01" android:text= "Modified" android:textstyle= "bold" android:typeface= "serif" > </TextView> <textview android:text= "modified" android:id= "@+id/textview1" android:textstyle= "Bold" Android: Typeface= "serif" ></TextView> </TableRow> <tablerow android:layout_height= "Wrap_content" Android: Id= "@+id/tablerow2" android:layout_width= "match_parent" > <textview android:text= "Day High" android:id= "@+id/" TextView2 "android:textstyle=" bold "></TextView> <textview android:id=" @+id/textview3 "android:text=" 28 °F "android:gravity=" Center_horizontal "></TextView> <textview android:text=" 26°f "android:id=" @+id/textview4 "android:gravity=" Center_horizontal "></TextView> <textview android: text= "23°f" android:id= "@+id/textview5" android:gravity= "Center_horizontal" ></TextView> <textview android:text= "17°f" android:id= "@+id/textview6" android:gravity= "Center_horizontal" ></TextView> < TextView android:text= "19°f" android:id= "@+id/textview7" android:gravity= "Center_horizontal" ></TextView> </TableRow> <tablerow android:layout_height= "wrap_content" android:id= "@+id/tablerow2" Android:layout_ Width= "Match_parent" > <textview android:text= "Day Low" android:id= "@+id/textview2" android:textstyle= "bold" ></TextView> <textview android:text= "15°f" android:id= "@+id/textview3" android:gravity= Horizontal "></TextView> <textview android:text=" 14°f "android:id=" @+id/textview4 "android:gravity=" Center_horizontal "></TextView> <textview android:text=" 3°f "android:id=" @+id/textview5 "Android:gravit"Y= "Center_horizontal" ></TextView> <textview android:text= "5°f" android:id= "@+id/textview6" Android: Gravity= "Center_horizontal" ></TextView> <textview android:text= "6°f" android:id= "@+id/textview7" Android:gravity= "Center_horizontal" ></TextView> </TableRow> <tablerow android:id= "@+id/ TableRow3 "android:layout_height=" wrap_content "android:layout_width=" match_parent "android:gravity=" Center "> <textview android:id= "@+id/textview8" android:text= "Conditions" android:textstyle= "bold" ></TextView> <imageview android:id= "@+id/imageview1" android:src= "@drawable/hot" ></ImageView> <imageview Android : id= "@+id/imageview2" android:src= "@drawable/pt_cloud" ></ImageView> <imageview android:id= "@+id/ IMAGEVIEW3 "android:src=" @drawable/snow ></ImageView> <imageview android:id= "@+id/imageview4" Android: src= "@drawable/lt_snow" ></ImageView> <imageview android:id= "@+id/imageview5" android:src= "@drawable/PT_sun "></ImageView> </TableRow></TableLayout>

Recall that in an activity, you simply add a line of code to the OnCreate () method to load and display the layout resources on the screen. If the layout resources are stored in the/res/layout/framed.xml file, this line of code should be:

Setcontentview (r.layout.table);

The table layout sets all columns to be compressed and stretched by using "*" in the value. If only specific columns need to be compressed or stretched, then a set of numbers separated by commas is required (the index of columns starts at 0).

The following figure is what the table looks like in the vertical and horizontal screens.

define table layouts with programs

You can also use programs to create and configure table layouts. This is accomplished by using the Tablelayout class and the TableRow class (Android.widget.TableLayout and Android.widget.TableRow). You will find unique display parameters for each control in Tablelayout.layoutparams and Tablerow.layoutparams. Similarly, typical layout parameters (Android.view.ViewGroup.LayoutParams), such as Layout_height and Layout_width, and margin parameters Number (Viewgroup.marginlayoutparams) can also be used on tablelayout and TableRow objects, but not in table cells. For table cells (any view in TableRow), the width is always match_parent. High can be defined, but the default value is wrap_content and no special designation is required.

Instead of using the Setcontentview () method to load a layout resource as shown earlier, you must create the screen content in Java and then provide a Setcontentview () method with a parent layout object that contains all the contents of the control to be displayed as a child view. Here, your parent layout is the table layout you created. For example, the following code example demonstrates how to instantiate a tablelayout layout parameter in an activity using a program and recreate the same layout as the previous XML description.

@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Tablelayout table = new Tablelayout (this); Table.setstretchallcolumns (TRUE); Table.setshrinkallcolumns (TRUE); TableRow rowtitle = new TableRow (this); Rowtitle.setgravity (gravity.center_horizontal); TableRow rowdaylabels = new TableRow (this); TableRow rowhighs = new TableRow (this); TableRow rowlows = new TableRow (this); TableRow rowconditions = new TableRow (this); Rowconditions.setgravity (Gravity.center); TextView empty = new TextView (this); Title Column/row TextView title = new TextView (this); Title.settext ("Java Weather Table"); Title.settextsize (Typedvalue.complex_unit_dip, 18); Title.setgravity (Gravity.center); Title.settypeface (Typeface.serif, typeface.bold); Tablerow.layoutparams params = new Tablerow.layoutparams (); Params.span = 6; Rowtitle.addview (title, params); Labels column TextView Highslabel = new TextView (this); Highslabel.settext ("Day High"); Highslabel.settypeface (TYPEFAce. Default_bold); TextView Lowslabel = new TextView (this); Lowslabel.settext ("Day Low"); Lowslabel.settypeface (Typeface.default_bold); TextView Conditionslabel = new TextView (this); Conditionslabel.settext ("Conditions"); Conditionslabel.settypeface (Typeface.default_bold); Rowdaylabels.addview (empty); Rowhighs.addview (Highslabel); Rowlows.addview (Lowslabel); Rowconditions.addview (Conditionslabel); Day 1 column TextView Day1label = new TextView (this); Day1label.settext ("Modified 7"); Day1label.settypeface (Typeface.serif, typeface.bold); TextView Day1high = new TextView (this); Day1high.settext ("28°f"); Day1high.setgravity (gravity.center_horizontal); TextView Day1low = new TextView (this); Day1low.settext ("15°f"); Day1low.setgravity (gravity.center_horizontal); ImageView day1conditions = new ImageView (this); Day1conditions.setimageresource (R.drawable.hot); Rowdaylabels.addview (Day1label); Rowhighs.addview (Day1high); Rowlows.addview (Day1low); Rowconditions.addview (day1conditions); Day2 ColuMN TextView Day2label = new TextView (this); Day2label.settext ("Modified 8"); Day2label.settypeface (Typeface.serif, typeface.bold); TextView Day2high = new TextView (this); Day2high.settext ("26°f"); Day2high.setgravity (gravity.center_horizontal); TextView Day2low = new TextView (this); Day2low.settext ("14°f"); Day2low.setgravity (gravity.center_horizontal); ImageView day2conditions = new ImageView (this); Day2conditions.setimageresource (R.drawable.pt_cloud); Rowdaylabels.addview (Day2label); Rowhighs.addview (Day2high); Rowlows.addview (Day2low); Rowconditions.addview (day2conditions); Day3 column TextView Day3label = new TextView (this); Day3label.settext ("Modified 9"); Day3label.settypeface (Typeface.serif, typeface.bold); TextView Day3high = new TextView (this); Day3high.settext ("23°f"); Day3high.setgravity (gravity.center_horizontal); TextView Day3low = new TextView (this); Day3low.settext ("3°f"); Day3low.setgravity (gravity.center_horizontal); ImageView day3conditions = new ImageView (this); Day3conditiOns.setimageresource (R.drawable.snow); Rowdaylabels.addview (Day3label); Rowhighs.addview (Day3high); Rowlows.addview (Day3low); Rowconditions.addview (day3conditions); Day4 column TextView Day4label = new TextView (this); Day4label.settext ("Modified 10"); Day4label.settypeface (Typeface.serif, typeface.bold); TextView Day4high = new TextView (this); Day4high.settext ("17°f"); Day4high.setgravity (gravity.center_horizontal); TextView Day4low = new TextView (this); Day4low.settext ("5°f"); Day4low.setgravity (gravity.center_horizontal); ImageView day4conditions = new ImageView (this); Day4conditions.setimageresource (R.drawable.lt_snow); Rowdaylabels.addview (Day4label); Rowhighs.addview (Day4high); Rowlows.addview (Day4low); Rowconditions.addview (day4conditions); Day5 column TextView Day5label = new TextView (this); Day5label.settext ("Modified 11"); Day5label.settypeface (Typeface.serif, typeface.bold); TextView Day5high = new TextView (this); Day5high.settext ("19°f"); Day5high.setgravity (Gravity.center_horizOntal); TextView Day5low = new TextView (this); Day5low.settext ("6°f"); Day5low.setgravity (gravity.center_horizontal); ImageView day5conditions = new ImageView (this); Day5conditions.setimageresource (R.drawable.pt_sun); Rowdaylabels.addview (Day5label); Rowhighs.addview (Day5high); Rowlows.addview (Day5low); Rowconditions.addview (day5conditions); Table.addview (Rowtitle); Table.addview (Rowdaylabels); Table.addview (rowhighs); Table.addview (rowlows); Table.addview (rowconditions); Setcontentview (table); }

Let's analyze the source code above. First we created a Tablelayout control and set the compression and extension property values of all columns to true with the Setstretchallcolumns () and Setshrinkallcolumns () methods. Next, we created five TableRow. Each TableRow contains a view control (title, date, TextView control that displays high temperature data, and a ImageView control that displays a weather chart). You can see how column stepover is handled in the first TableRow. Each TableRow is added to the Tablelayout control sequentially with the AddView () method. Finally we load the tablelayout and use the Setcontentview () method to display it in the interface.

As you can see, the amount of code grows quickly as more and more controls are added to the interface. For ease of organization and maintainability, it is best to use a program to define and use a layout in a special case rather than a general situation. Also, in similar cases, the data is usually from other sources rather than the strings we write ourselves, so loops may be more appropriate for many programs.

The following figure shows the results of the operation. As you expected, the results are the same as the above.

Table Layout Essentials

Although you can design the entire user interface with a table layout diagram, this is usually not the best tool because tabular layouts (tablelayout) inherit from linear layouts (linearlayout) and not the most efficient layout controls. Careful scrutiny can be found that tablelayout is nothing more than a series of linearlayout ordered nesting, and performance considerations generally do not encourage the nesting level too deep. However, tabular layouts can be a reasonable choice for data that is in tabular form, such as spreadsheet data.

In addition, table layout data changes with screen size and resolution size. You need to set the scroll bar when you need to display large amounts of data. For example, if you need to include an assessment of weather conditions in the above example, the text may be a sentence or even 20 sentences, so setting a vertical or horizontal scroll bar is a wise choice.

Summary

The Android program uses layouts to define the user interface, and the table layout works well with view data and controls that are displayed in rows and columns. Reasonable use of table layout can make the interface design easy and quick. However, it is also clear that tabular layouts inherit from linear layouts and therefore have many performance limitations like linear layouts. [中文版]

Reprint Please specify:
Author: rockux–web Front end
From: Android user interface design: Table layout

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.