Android Beginner 2016 (7)--layout

Source: Internet
Author: User

Layout, this in the service has become no, but also the service side of the people to learn the client of a hurdle bar.

Once used Cocos2d-x to write a small game is this very difficult to understand, or can use, but not much understanding, very difficult to write a good layout, difficult to fit the application of commercialization.

The layout of the game is a bit like using Photoshop to draw a feeling. The concept of an existing scene, like a canvas, is then layered on top of it. Be able to operate on a separate layer. When displayed, it is possible to think that the picture is piled up from the bottom to the top layer. There are a lot of elves inside the layer, and the elves are the people we see moving, the architecture, the weird stuff. This is probably the design idea of cocos2d.

I have always believed that, learning programming, must want to understand people's design ideas, understand, follow the thought. Smooth.

Not understand. is a big maze, here a door, there is a door, like everywhere can go. Can lead to success, but some of the path seems to be achieved, in fact, more than a multiplier. The same, we do the function, do the application, is also the same, first think good ideas, and then talent to do. Perhaps this is the University of the time to learn the outline of the design, has been doing so, may have learned the theory is a bit of a use.

This is the first thought. Let's go and have a look at someone else's blog. Finally, there may be several articles in the following. Go back and summarize the comparison.

What is the layout?

Layout is the base of the Android program container, you can put a variety of controls in, their own initiative in a specific way to make a decision.

What are the layout methods?

LinearLayout, Rleativelayout, Tablelayout, framelayout, etc., can be declared by means of an XML class. Or the way you encode it. XML declarations allow for better separation of code and views, no compilation after changes, and easier support for different screen sizes.


In this case, you should make it clear that the layout alone is invisible and needs to be represented by other components. Since it is an introduction, it is difficult to understand the abstract things that come up with me.

Rleativelayout:

First a few can see, I think. button, text, input box. Should be relatively simple, can see. Let's go and see what we can see.

First look at the layout file, where is the file? Take a look at the project structure description

Watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqv/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/dissolve/70/gravity /southeast ">

The layout file is below the res/layout/folder, Activity_hello_world.xml. Here by the way. The file name must be in lowercase. Different words are separated by ' _ '.

Open the file, click on the graphical layout in the diagram to see a visual layout interface, the ability to select different components, and then drag and drop.

We can see that there is already a string on top of "Hello world!".

Now add some more text, button, enter the box to see it.

This should show how the layout works.


When dragging, a variety of tips for aligning other components appear. I'll put it in a random. And then it was very messy.


Casually pull some buttons and text. Pull past will show up, would like to pull an input box. But it seems to be an error. Some operations may be incorrect. Just the layout of today's theme, so don't study the details of the text, button these controls first. The interface is the same after the execution of the program.

The text. Button messy, I arbitrarily pull, as if put on where the.

Let's look at the XML code of the layout file first. Why would it be this.

<!--relative layout, the ability to specify the position of the child element relative to the parent element or other child elements.

This is one of the more frequently used layouts.

Xmlns:android the namespace of the XML. By telling the eclipse related properties here Android:layout_width the current width of this layout, android:layout_height the current height of the layout is match_parent. That is, the parent element is populated. This layout is already the outermost layer. So it fills the entire phone screen. Tools:context associated with the specified activity, a layout file can normally be used in multiple places. This is a declaration of a specific context. Assume that a theme is specified and can be rendered. --><relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context = "${relativepackage}.${activityclass}" ><!--text, "wrap_content" adaptive size, forcing the display of all the contents of the text. The width and height are all here. Android:text text content, where the @string is filled in, indicating that the content is within Res/values/strings.xml, and that the name is Hello_wrold attribute.

Android:id defines the ID of this testview.

The corresponding ID is generated within the R.java. When writing Java code, you can get this text via R.id.textview2--<textview android:layout_width= "Wrap_content" Android:layo ut_height= "Wrap_content" android:text= "@string/hello_world" android:id= "@+id/textview2"/><!-- This is a manual pull-in TextView, almost the same as above. There are a few more properties. Layout_alignparentleft is left-aligned relative to the parent control. Layout_below is set to the following for a control. This is filled out below textView2. Android:text directly fills in the text content, can pass the ID, with Strings.xml--<textview android:id= "@+id/textview1" Android:layo Ut_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignparentleft= "true" an droid:layout_below= "@+id/textview2" android:text= "TextView"/> <button android:id= "@+id/button1" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_alignparentleft = "true" android:layout_below= "@+id/textview1" android:layout_margintop= "16DP" android:text= "button"/ > <button Android: id= "@+id/button2" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:l Ayout_alignbottom= "@+id/button1" android:layout_marginleft= "27DP" android:layout_torightof= "@+id/button1" android:text= "button"/> <button android:id= "@+id/button3" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:layout_alignbaseline= "@+id/button2" Android:layout_alig Nbottom= "@+id/button2" android:layout_torightof= "@+id/button2" android:text= "button"/> <TextView Android:id= "@+id/textview3" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:layout_alignright= "@+id/button2" android:layout_below= "@+id/button1" Android:layout_marginrigh t= "32DP" android:layout_margintop= "23DP" android:text= "Medium text" android:textappearance= "?

Android:attr/textappearancemedium "/> <textview android:id=" @+id/textview4 "Android:layout_width=" W Rap_content "android:layout_height=" wrap_content "android:layout_alignleft=" @+id/button3 "Android:lay out_below= "@+id/textview3" android:text= "Large text" android:textappearance= "? Android:attr/textappearancelar GE "/></relativelayout>

With this XML file, we can see that the layout component has a lot in common with TextView, Button, and so on.

They have very many common attributes. These properties have default values, and very many are not configurable. And very much, the proposal used to go to Baidu.

To summarize, the properties that come into contact today are mainly displayed text content and placement. and the ID.

Now, with a preliminary impression, we can look at other layouts (almost forgetting that today is the layout). Uh...

)


LinearLayout:

Changed the rleativelayout directly. Layout_alignparentleft These are clearly relative positional properties, all warnings are illegal. Remove the things that are clearly relative to the layout.

<linearlayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Android:orien tation= "vertical" ><!--android:orientation for directional properties. Vertical are arranged vertically. So the layout component arranges all the child elements vertically.

--<textview android:layout_width= "wrap_content" android:layout_height= "Wrap_content" and roid:text= "@string/hello_world" android:id= "@+id/textview2"/> <textview android:id= "@+id/textView1" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/text1"/&gt ; <button android:id= "@+id/button1" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "android:text=" @string/text2 "/> <button android:id=" @+id/button2 "Android:layout_w Idth= "Wrap_content" android:layout_height= "wrap_content" android:text= "@string/text3"/> <Button Android:id= "@+id/button3" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:text= "@string/text4"/> <textview android:id= "@+id/textview3" android:layout_width= "Wra P_content "Android:layout_height= "Wrap_content" android:text= "@string/text5"/> <textview android:id= "@+id/te XtView4 "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:text=" @str Ing/text6 "/></linearlayout>

Can see, very neat, and vertical in a row.

Tablelayout:

It feels like the HTML table, the entire component is made into a single table.

Its elements are divided into rows, TableRow. The following is a detailed element. The width of the child element is not controllable and the height can vary according to itself.

 <tablelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Wrap_content "Android:orien tation= "Vertical" > <!--is divided into one line with TableRow.            --<tablerow android:id= "@+id/tablerow1" android:layout_width= "Wrap_content"                android:layout_height= "Wrap_content" > <button android:id= "@+id/button4" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Butt On1 "/> <button android:id=" @+id/button5 "android:layout_width=" Wrap_conten                T "android:layout_height=" wrap_content "android:text=" Button2 "/> <button Android:id= "@+id/button7" android:layout_width= "Wrap_content" Android:layout_height= "Wrap_content" android:text= "Button3"/> </TableRow> <tablerow Android:id= "@+id/tablerow2" android:layout_width= "wrap_content" android:layout_height= "wrap_content "> <textview android:id=" @+id/textview5 "android:layout_width=" Wrap_conten T "android:layout_height=" wrap_content "android:text=" TextView "/> <textvie W android:id= "@+id/textview6" android:layout_width= "Wrap_content" Android:lay out_height= "Wrap_content" android:text= "TextView"/> <textview android:id= "                @+id/textview7 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "                    android:text= "Large text" android:textappearance= "? Android:attr/textappearancelarge"/> </tablerow> <tablerow android:id= "@+id/tablerow3" android:layout_width= "Wrap_content"                android:layout_height= "Wrap_content" > <textview android:id= "@+id/textview9" Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "TextView"/> <textview android:id= "@+id/textview10" android:layout_width= "                Wrap_content "android:layout_height=" wrap_content "android:text=" Large text " Android:textappearance= "? Android:attr/textappearancelarge"/> <button android:id= "@+id/but                Ton8 "android:layout_width=" wrap_content "android:layout_height=" Wrap_content "            android:text= "button"/> </TableRow> <tablerow android:id= "@+id/tablerow4" Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" > </TableRow> </TableLayout> 


Framelayout:

all child elements will be fixed in the upper left corner of the screen ; cannot be a Framelayout specifies a location for a child element in the But it can control its position through the child control itself.

The latter child element will overwrite the fill directly above the previous child element. Block them partially or all ( unless the latter child element is transparent ).

This layout is often used for games or to process some gallery programs.

From being able to see. The three buttons are stacked together.


 < Framelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http://schemas.android.com/ Tools "Android:layout_width=" Match_parent "android:layout_height=" wrap_content "android:orientation=" vertical "         ; <button android:id= "@+id/button4" android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" android:text= "Button1"/> <button android:id= "@+id/button5" android:l Ayout_width= "Wrap_content" android:layout_height= "wrap_content" android:text= "Button2"/> <Bu Tton android:id= "@+id/button7" android:layout_width= "wrap_content" android:layout_height= "Wrap_ Content "android:text=" Button3 "/></FRAMELAYOUT> 

The layout is here as a temporary introduction. To be precise, let me get started. It is not necessary to drill down, the most important thing is to understand what layout is, there is a general understanding, will use, this is the first step of learning. It is recommended that you copy the code to your eclipse to see it. Can be understood more deeply.



Android Beginner 2016 (7)--layout

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.