Start from scratch to learn android<tablelayout table layouts. 15 .>

Source: Internet
Author: User

Tablelayout is the display of the cell phone's screen in a row, and a row can be multiple controls

And you can set the alignment of the control, and whether it is a shrink line

Let's look at the use of the Tablelayout layout using a single line diagram and a simple example.


.................................................................. There is no beauty in the dividing line ... ..... ..... ..... ..... ..... .......-.....-......................

Configure using an XML file alone

<?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:shrinkcolumns=" 3 ">// Line three allows row merge <TableRow> <textview android:id= "@+id/text01" android:layout_width=            "Wrap_content" android:layout_height= "wrap_content" android:text= "user name"/> <edittext Android:id= "@+id/edit01" android:layout_width= "wrap_content" android:layout_height= "Wrap_con    Tent "android:ems=" > <requestfocus/> </EditText> </TableRow>            <TableRow> <textview android:id= "@+id/text02" android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "password"/> <edittext andro Id:id= "@+id/edittext1" AndroId:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:ems= "a"            Ndroid:inputtype= "Textpassword"/> </TableRow> <tablerow android:gravity= "center" > <button Android:id= "@+id/button" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "android:text=" login System "/> </TableRow></TableLayout>
Run


From the above, you can see clearly that the second line of the first line shows two components, the third line shows a component, and sets the effect of alignment in the play.

.................................................................. There is no beauty in the dividing line ... ..... ..... ..... ..... ..... .......-.....-......................

The dynamic layout is implemented in a way.

Package Com.example.tablelayout;import Android.app.activity;import Android.os.bundle;import android.view.Gravity; Import Android.view.viewgroup;import Android.widget.button;import Android.widget.edittext;import Android.widget.tablelayout;import Android.widget.tablerow;import Android.widget.textview;public class MainActivity Extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);// Setcontentview (r.layout.tablelayout); Tablelayout layout = new Tablelayout (this);//Create Tablelayout object//Set Tablelayout alignment Tablelayout.layoutparams Tabletlayoutparams = new Tablelayout.layoutparams (viewgroup.layoutparams.match_parent,viewgroup.layoutparams.wrap _content); TableRow tablerow1=new TableRow (this);//Create First row TableRow tablerow2=new TableRow (this);//create second row TableRow tablerow3=new TableRow (this);//Create a third line TextView textview1=new TextView (this);//Create TextView Object Textview1.settext ("username"); EditText edittext1=new EditText (this);//Create EditText Object Tablerow1.addview (TextView1);TextView set to the first line Tablerow1.addview (EDITTEXT1);//edittext set to the first row TextView textview2=new TextView (this);// Create TextView Object Textview2.settext ("password"); EditText edittext2=new EditText (this);//Create EditText Object Tablerow2.addview (TEXTVIEW2);// TextView set to the first line Tablerow2.addview (EDITTEXT2);//edittext set to the second row button Button=new button (this); Button.settext ("Login System") ; tablerow3.setgravity (gravity.center);//set line nine to align Tablerow3.addview (button); Layout.addview (TABLEROW1);// Add the first row of data to the layout Layout.addview (TABLEROW2);//Add the second row of data to the layout Layout.addview (TABLEROW3);// Add the third row of data to the layout Super.setcontentview (Layout,tabletlayoutparams);}}


Dynamic layout can also achieve the effect of XML layout implementation


Forecast for the next section: Relative layout relativelayout


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.