Android Combat Simple tutorial-the 13th gun (five layout studies)

Source: Internet
Author: User
<span id="Label3"></p>We know that Android applications are typically composed of multiple activity, and these activity is presented in the form of a view in front of us,<span>a <span>view is composed of one component. Components are our common button, textedit, and so On. So what do we usually see in the Android phone those beautiful interface is how to display</span></span><span><span>out of it? This is going to use the Android layout manager, The online analogy is very good: the layout is like the framework of the building, the components in accordance with the layout of the requirements in order to form a beautiful interface for the See. </span></span><p><p><span>Before you analyze the layout, Let's take a look at the Controls: any visual controls in Android are inherited from android.veiw.View, and there are two ways to set up the View: the first is our most common use of XML files to configure the properties of the view, and then when the program starts, the system creates the corresponding V from the configuration File. Iew VIEW. The second is that we use the corresponding class directly in the code to create the VIEW.</span></p></p><p><p><span><span style="color:#ff00">How to define a view using an XML file:</span></span></p></p><p><p><span>Each Android project has a res/layout directory under the source directory, which is used to store layout files. Layout files are typically named after the Activity's name, with. XML as the Suffix. When creating a component in xml, you need to specify an ID for the component, such as: the android:id= "@+id/name" system automatically creates the corresponding R resource class variable in the Gen Directory.</span></p></p><p><p><span><span style="color:#ff00">How to use views in your code <span></span> :</span></span></p></p><p><p><span>when you create each activity in your code, you typically call Setcontentview () in the OnCreate () method to load the specified XML layout file, and then you can pass the Findviewbyid () To get a control of the corresponding ID created in the layout file, such as Button. </span></p></p><p><p><span>such as:</span></p></p><pre class="java" name="code"><pre class="java" name="code">Private Button btnsndmag;public void onCreate (Bundle Savedinstancestate) { super.oncreate (savedinstancestate); Setcontentview (r.layout.main);//load main.xml layout file btnsndmag = (Button) This.findviewbyid (r.id.btnsndmag);// Find the button component for by id ... .}</pre></pre><br>Let's describe the five major layouts available to us in android: linearlayout (linear layout), framelayout (single frame layout), absolutelayout (absolute layout), tablellayout (table layout), Relativelayout (relative layout). The most commonly used ones are linearlayout, tablellayout and Relativelayout. These layouts can be used in nested nesting.<p><p><span><strong><span style="color:#cc00">(1) linearlayout Linear layout</span></strong></span></p></p><p><p><span>A linear layout is a sequence of child elements (which can be controls or layouts) sequentially, in either a horizontal or vertical order, with each element positioned after the previous Element. Linear layouts are divided into two types: horizontal and Vertical. Set by property android:orientation= "vertical" and android:orientation= "horizontal" respectively.</span></p></p><p><p><span>Android:layout_weight represents the proportion of space that a child element occupies, and some say that the value is proportional to the amount of space occupied, some say inversely. I set in the actual application and the online data display just the opposite, This problem will be written in a special article to Analyze. Now all we have to do is set it in positive proportions.</span></p></p><p><p><span>for example, Let's implement a simple calculator interface:</span></p></p><pre class="html" name="code"><linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:orientation=" Vertical "android:layout_width=" match_parent "android:layout_heig ht= "match_parent" android:background= "#FFFFFF" tools:context= ". Mainactivity "><!--//the First line here shows the label as a horizontal layout--<linearlayout android:layout_width=" match_parent " android:layout_height= "wrap_content" android:orientation= "horizontal" > <edittext androi d:id= "@+id/msg" android:inputtype= "number" android:layout_width= "match_parent" Android:la yout_height= "wrap_content" android:text= "" > </EditText> </LinearLayout> <!- -//second Behavior MC m+ m-mr four buttons form a horizontal layout--<linearlayout android:layout_width= "match_parent" Andro id:layout_height= "wrap_content" android:orientation= "horizontal" > <buttonAndroid:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "mc" android:layout_ weight= "1" > </Button> <button android:layout_width= "match_parent" Android:layout_heigh t= "wrap_content" android:text= "m+" android:layout_weight= "1" > </Button> <button androi D:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "m" android:layout_weight = "1" > </Button> <button android:layout_width= "match_parent" android:layout_height= "wra P_content "android:text=" Mr "android:layout_weight=" 1 "> </Button> </LinearLayout> <!-- Ditto C +/-/* four buttons form a horizontal layout--<linearlayout android:layout_width= "match_parent" Android oid:layout_height= "wrap_content" android:orientation= "horizontal" > <button android:l Ayout_width= "match_parenT "android:layout_height=" wrap_content "android:layout_weight=" 1 "android:text=" C " > </Button> <button android:layout_width= "match_parent" Android:la yout_height= "wrap_content" android:layout_weight= "1" android:text= "+/-" > </butto n> <button android:layout_width= "match_parent" android:layout_height= "wrap_conten T "android:layout_weight=" 1 "android:text="/"> </Button> <button Android:layout_width= "match_parent" android:layout_height= "wrap_content" Android:la yout_weight= "1" android:text= "*" > </Button> </LinearLayout> <linear Layout android:layout_width= "match_parent" android:layout_height= "wrap_content" android:orientation= " Horizontal "> < Button android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "7" A ndroid:layout_weight= "1" > </Button> <button android:layout_width= "match_parent" Androi d:layout_height= "wrap_content" android:text= "8" android:layout_weight= "1" > </Button> <button Android:layout_width= "match_parent" android:layout_height= "wrap_content" android:text= "9" android:l ayout_weight= "1" > </Button> <button android:layout_width= "match_parent" android:layout _height= "wrap_content" android:text= "-" android:layout_weight= "1" > </Button> </LinearLayout> <linearlayout android:layout_width= "match_parent" android:layout_height= "wrap_content" Android : orientation= "horizontal" > <button android:layout_width= "match_parent" android:layout_ height= "wrap_content" android:layout_weight= "1" android:text= "4" > </Button> <button A Ndroid:layout_width= "match_parent" android:layout_height= "wrap_content" android:layout_weight= "1" android:text= "5" > </Button> <button android:layout_width= "match_parent" android:layout_height= "wrap_content" android:layout_weight= "1" android:text= "6" > </Button> <button android:layout_width= "match_parent" android:layout_height= "wrap_co Ntent "android:layout_weight=" 1 "android:text=" + "> </Button> &LT;/LINEARLAYOUT&G T <!--//outermost layer is a horizontal layout, from the left top row 1 2 33 buttons, 0 of the following LINE. Two button and right = Composition--<linearlayout android:orientation= "horizontal" android:layout_width= "match_parent" android:layout_height= "wrap_content" > <!--//here 1 2 3 and below 0. constitute a verticalStraight layout--<linearlayout android:orientation= "vertical" android:layout_weight= "3" android:layout_w idth= "wrap_content" android:layout_height= "wrap_content" > &LT;!--//1 2 3 here to form a horizontal layout--<line Arlayout android:orientation= "horizontal" android:layout_width= "match_parent" android:layout_height= "wrap_co Ntent "> <button android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:layout_weight=" 1 "android:text=" 1 "></Button> <button Android : layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_weight= "1" android:text= "2" ></Button> <button android:layout_width= "wrap_content" Android : layout_height= "wrap_content" android:layout_weight= "1" android:text= "3" ></Button> < /linearlayout> <! --//here's 0 And. form a horizontal layout, note here The Android_weight parameter is set--<linearlayout android:orientation= "horizontal" android:layout_width= "match_parent" android:layout_height= "wrap_content" > <button android:layout_width= "0px" android:layout_height= "wrap_content" android:layout_weight= "2" android:text= "0" ></butt on> <button android:layout_width= "0px" android:layout_height= "wrap_content" android:layout_weight= "1" android:text= "." ></Button> </LinearLayout> </LinearLayout> <!--//a Vertical layout consisting of a single Button here--&lt ; LinearLayout android:orientation= "vertical" android:layout_weight= "1" android:layout_width= "wrap_content" android:layout_height= "match_parent" > <button android:layout_width= "match_parent" android:l ayout_height= "match_parent" android:text= "=" ></Button> </LinearLayout> </LinearLayout> </LinearLayout> </pre><p><p><br><strong><span style="color:#cc00">(2) tablelayout Table Layout</span></strong></p></p><p><p><span>Table layout, suitable for multi-row multi-column layout format, Each tablelayout is composed of more than one tablerow, a tablerow represents each row in the tablelayout, this line can be composed of multiple child elements. In fact, both Tablelayout and TableRow are subclasses of the linelayout linear Layout. however, the TableRow parameter Android:orientation property value is fixed to horizontal, and Android:layout_width=match_parent,android:layout_height=wrap _content. So TableRow is actually a horizontal linear layout, and so the child elements are the same width and height.</span></p></p><p><p><span>note: in tablelayout, cells can be empty, but cannot span columns, meaning that only adjacent cells cannot be Empty. </span></p></p><p><p><span>In a tablelayout layout, the width of a column is specified by the widest cell in the column, and the width of the table is specified by the parent container. You can set the following properties for each column:</span></p></p><p><p><span>shrinkable indicates that the width of the column can be shrunk so that the table adapts to the size of the parent container</span></p></p><p><p><span>stretchable indicates that the width of the column can be stretched so that it fills the free space in the table</span></p></p><p><p><span>collapsed indicates that the column will be hidden</span></p></p><p><p><span>unique properties in Tablelayout:</span></p></p><p><p><span>Android:collapsecolumns</span></p></p><p><p><span>Android:shrinkcolumns</span></p></p><p><p><span>Android:stretchcolumns = "0,1,2,3"</span></p></p><p><p><span>Demo: We want to design a table with three rows and three columns as follows, but the second line we just want to show 2 tables:</span></p></p><p><p></p></p><pre class="html" name="code"><?xml version= "1.0" encoding= "utf-8"? ><tablelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:orientation=" Vertical "android:shrinkcolumns=" 0,1,2 "//set Three columns can be shrunk android:stretchcolumns=" 0,1 , 2 "//set Three columns can be stretched if you do not set this, the displayed table will not be able to fill the entire screen android:layout_width=" fill_parent "android:layout_height=" fill_parent " ; <tablerow android:layout_width= "fill_parent" android:layout_height= "wrap_content" > <Button android: gravity= "center" android:padding= "10dp" android:text= "Button1" > </Button> <button android:gravity= "center" android:padding= "10dp" android:text= "Button2" > < /button> <button android:gravity= "center" android:padding= "10dp" android:text= "Button3" > </Button> </TableRow> <tablerow android:layout_width= "fill_parent" android:layout_ height= "wrap_content" > <button android:gravity= "center" android:padding= "10dp" android:text= "Button4" > & lt;/button> <button android:gravity= "center" android:padding= "10dp" android:text= "Button5" > </Button> </TableRow> <tablerow android:layout_width= "fill_parent" Androi d:layout_height= "wrap_content" > <button android:gravity= "center" android:padding= "10dp" android:text= "Button6" > </Button> <button android:gravity= "center" android:padd ing= "10dp" android:text= "Button7" > </Button> <button android:gravity= "center" android:padding= "10dp" android:text= "Button8" > </Button> </tablerow></tablelayo Ut></pre><p><p><strong><span style="color:#cc0000">(3) relativelayout Relative layout</span></strong><span style="color:#cc0000"></span></p></p><p><p><span>Relativelayout inherits from android.widget.ViewGroup, which completes the layout according to the position relationship between child elements, as the most flexible and most commonly used layout in the five layouts of the Android system, it is very suitable for some complicated interface design.</span></p></p><p><p><span>note: before referencing other child elements, the ID of the reference must already exist, or an exception will Occur. </span></p></p><p><p><span>Common location Properties:</span></p></p><pre class="html" name="code"><pre class="html" name="code">Android:layout_toleftof the component is located on the left side of the reference component Android:layout_torightof the component is on the right side of the reference component Android:layout_above The component is located above the reference component Android:layout_below the component is located below the reference component Android:layout_alignparentleft the component is aligned to the left side of the parent component of Android: Layout_alignparentright whether the component is aligned to the right end of its parent component Android:layout_alignparenttop whether the component aligns to the top of the parent component Android:layout_ Alignparentbottom Whether the component aligns to the bottom of the parent component Android:layout_centerinparent The component is centered relative to the parent component Android:layout_ Centerhorizontal Whether the component is centered horizontally android:layout_centervertical whether the component is vertically centered</pre></pre><p><p>Example:</p></p><pre class="html" name="code"><?xml version= "1.0" encoding= "utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "android:layout_width=" fill_parent "android:layout_height=" fill_parent "> <button android:id = "@+id/btn1" android:layout_width= "wrap_content" android:layout_height= "wrap_content" android:layout_ centerhorizontal= "true" android:layout_centerinparent= "true" android:text= "Button1" > </Button> <button android:id= "@+id/btn2" android:layout_width= "wrap_content" android:layout_height= "wrap_ Content "android:layout_above=" @id/btn1 "android:layout_toleftof=" @id/btn1 "android:text=" Button2 "&G T </Button> <button android:id= "@+id/btn3" android:layout_width= "wrap_content" Android:layo ut_height= "wrap_content" android:layout_above= "@id/btn1" android:layout_torightof= "@id/btn1" android: text= "Button3" > </button> <button android:id= "@+id/btn4" android:layout_width= "wrap_content" android:layout_h eight= "wrap_content" android:layout_above= "@id/btn2" android:layout_toleftof= "@id/btn3" android:layou t_torightof= "@id/btn2" android:text= "Button4" > </Button></RelativeLayout></pre><p><p><br></p></p><p><p><strong><span style="color:#cc0000">(4) framelayout Frame Layout</span></strong><span style="color:#cc0000"></span></p></p><p><p><span>put all the child elements in the upper-left corner of the entire interface, the following child elements directly overwrite the previous child elements, so use Less. </span></p></p><p><p><span><span style="color:#cc00"><strong>(5) absolutelayou Absolute Layout</strong></span></span></p></p><p><p>the <span>Absolute layout of all the child elements by setting the android:layout_x and android:layout_y properties, The coordinate position of the child element is fixed down, that is, coordinates (android:layout_x, android:layout_y), The layout_x is used to denote the horizontal axis and layout_y to indicate Ordinate. </span>the <span>upper left corner of the screen is the coordinate (0,0), the horizontal to the right square, the vertical downward for the Square. In practical applications, this layout is relatively small, because the Android terminal is more general models, the respective screen size. resolution, etc. may not be the same, if the absolute layout, may result in some of the terminal display is not Congruent. </span></p></p><p><p><span></span></p></p><p><span><span style="color:#ff00">in <span style="color:#ff00"> <strong>addition to the properties of several layouts commonly used above:</strong> </span></span><br><strong><strong>(1) Layout_margin</strong></strong> <br>Used to set the margin of the Control's edge relative to the parent control<br>Android:layout_marginleft<br>Android:layout_marginright<br>Android:layout_margintop<br>Android:layout_marginbottom<br><br><strong><strong>(2) layout_padding</strong></strong> <br>Used to set the padding of the Control's contents relative to the edge of the control<br>Android:layout_paddingleft<br>Android:layout_paddingright<br>Android:layout_paddingtop<br>Android:layout_paddingbottom<br><br><strong><strong>(3) layout_width/height</strong></strong> <br>Used to set the height and width of the control<br>Wrap_content content wrap that represents the text size padding for this control<br>Fill_parent Follow parent window<br>Match_parent<br><br><strong><strong>(4) Gravity</strong></strong> <br>Used to set the alignment of content within the view component<br>Top bottom Left right center, etc.<br><br><strong><strong>(5) android:layout_gravity</strong></strong> <br>Used to set the alignment of the container component<br>Android:layout_aligntop aligns the top edge of this element with the top edge of an element<br>Android:layout_alignleft aligns the left edge of this element with the left edge of an element<br>Android:layout_alignbottom aligns the bottom edge of this element with the bottom edge of an element<br>Android:layout_alignright aligns the right edge of this element with the right edge of an element<br></span></p><p><p><br><br></p></p><p><p>Android Combat Simple tutorial-the 13th gun (five layout studies)</p></p></span>

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.