Android Learning (iv)--android layout mode

Source: Internet
Author: User

In addition to the controls we talked about above, there are actually a lot of other things. Basically, we talk about the basics, so other controls require learners to learn in a later production. In addition to these components, we also need something to include these components, which is what we call the layout.

There are four basic layout modes in Android: LinearLayout, Relativelayout, Tablelayout, Framelayout

—————————————————--the dividing line of sparkly ——————————————————————

1. LinearLayout

First, let's look at LinearLayout first. This layout is very common in Android apps. This type is like queuing in our lives (assuming that the individual is a component). So when we're in line, we can row sideways or vertical. Next, let's analyze the properties in LinearLayout.

<xmlns:android= "http://schemas.android.com/apk/res/android"    Android : layout_width= "Match_parent"    android:layout_height= "Match_parent"     android:orientation = "Horizontal"    > </ LinearLayout >

There are a lot of properties in LinearLayout, I often use android:layout_height,android:layout_width and android:orientation. One of the last attributes we have not seen above.

Android:orientation describes the direction of the layout of LinearLayout. That is, we want each component to be lined up or lined up vertically. In this attribute, it provides two options: vertical and horizontal, which correspond to which direction to queue.

In addition to these properties set in the layout, we can also set the position of the control in this layout in the various spaces in the linear layout, and set the property name to Android:layout_gravity. Top,bottom,center_vertical and so on, exactly where you need to make your choice in the options.

2. Relativelayout

This layout is called relative layout. In this layout, we use properties of relative positions to control the relative position of the component in the layout and the relative position of the component with the component. Let's first look at the position of the component relative to the layout.

Relative position of the component relative to the layout:

Among the properties are Android:alignparentleft, Android:alignparentright, Android:alignparenttop, Android:alignparentbottom, Android:centerinparent These properties sets the relative position of the component relative to this layout.

In a specific use, such as android:alignparentright= "true", the component will be close to the right edge of the parent class. The properties here can be used in conjunction.

Relative position properties of the control:

These include Android:torightof, Android:layout_above, Android:toleftof, Android:layout_below, and so on to control the positioning relative to a control. Similar to the relative position of the layout, these properties can also be used in conjunction, as shown in the following settings:

For example: android:layout_below= "@id/button2"

Using android:layout_alignleft means that the left edge of one control is aligned with the left edge of another control, the same attributes are android:layout_alignright, and so on.

3. Framelayout

Frame is meant to be framed, so let's call it a frame layout. As mentioned in the first line of code, this type of layout is not used much. Its main function is to overwrite some components, which are added later on top of the components above. We'll use it further when we make The Shard (fragment), which is just a brief explanation.

4. Tablelayout

Layout of the table type. This type is like a table, where you need to place a component into a space in the table. I looked for it, as if for a space in a single table we can't set it to size. has a uniform size for each column. Now let's look at how the layout file is written:

<Tablelayoutxmlns:android= "Http://schemas.android.com/apk/res/android"Android:layout_width= "Match_parent"Android:layout_height= "Match_parent">    <TableRow>        <TextViewAndroid:layout_height= "Match_parent"Android:text= "Account:"/>        <EditTextAndroid:id= "@+id/account"Android:layout_height= "Wrap_content"Android:hint= "Input"/>    </TableRow>    <TableRow>        <TextViewAndroid:layout_height= "Match_parent"Android:text= "Password:"/>        <EditTextAndroid:id= "@+id/password"Android:layout_height= "Wrap_content"Android:inputtype= "Textpassword"/>    </TableRow>    <TableRow>        <ButtonAndroid:id= "@+id/login"Android:layout_height= "Wrap_content"Android:layout_span= "2"Android:text= "Login"/>    </TableRow></Tablelayout>

First we create the table layout, and then we need to add elements to the table layout. The first thing we want to add is the row element, the <TABLEROW>, and any component added here will appear in the same row. Each component occupies a single column. In this layout, each addition of a tablerow element represents a row added to the table. TableRow does not specify a width.

Because you do not specify a width, any component is suitable for the size of the component, and there is a problem that the full screen cannot appear, which can greatly affect the aesthetics of the app, so there is a property in Tablelayout that specifies that a column be stretched, that is, android:stretchcolumn = "1", this property means that if the screen does not have a full screen, the second control is elongated.

P.S. In the layout, we can adjust the offset position of each control in the layout:

Android:layout_marginxxx This property controls the offset distance in the left and right direction of a control, with detailed

Android:layout_marginleft the offset in the specific direction.

5. Custom Layouts

In addition to the four layouts described above, there are many custom layouts. For example, we add sub-layouts to the layout. So let's write the child layout first and then reference it in the parent layout:

    <layout= "@layout/external_layout"/>

We need to add some buttons and other components that need feedback in our custom layouts, and we can't just let them fool around the layout without reacting. In the following sections we will customize the specific layout. We set the linear layout according to the examples in the book.

Create a type that inherits from some of the underlying 4 layouts.

 Public classTitlelayoutextendsLinearLayoutImplementsview.onclicklistener{ PublicTitlelayout (Context context, AttributeSet attrs) {Super(Context,attrs); Layoutinflater.from (context). Inflate (R.layout.external_layout, This); Button Titleback=(Button) Findviewbyid (r.id.title_back); Button Titleedit=(Button) Findviewbyid (R.id.title_edit); Titleback.setonclicklistener ( This); Titleedit.setonclicklistener ( This); } @Override Public voidOnClick (View v) {Switch(V.getid ()) { Caser.id.title_back: (Activity) GetContext ()). Finish ();  Break;  CaseR.id.title_edit:toast.maketext (GetContext (),"You click Edit button", Toast.length_short). Show ();  Break; default:        }    }}

After we have written the custom layout, we also need to add it to the parent layout so that the app can be displayed, as shown in the following:

  < com.carels.chapterthree.TitleLayout         Android:layout_height = "Wrap_content"         android:layout_width= "match_parent"/>

Almost the layout is so many, can design a good layout of the people are genius ah. What we need is to draw inspiration from the good design of others and design our own beautiful layout.

Android Learning (iv)--android layout mode

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.