Android notes (vi) layout in android-linear layout

Source: Internet
Author: User
<span id="Label3"></p><p align="left"><p align="left">Our software is made up of a good number of interfaces, and each interface is made up of n multiple controls, with the help of layouts in Android to allow each space to be placed in an orderly manner on the Interface.</p></p><p align="left"><p align="left">The layout can be thought of as a container that can place a lot of controls, and it can adjust the position of the control according to certain rules, so as to achieve a beautiful interface.</p></p><p align="left"><p align="left">Layouts can also be placed in the layout, through the nesting of multiple layers of layout, to achieve a more complex interface.</p></p><p align="left"><p align="left">Android offers four basic layouts: linearlayout, relativelayout, framelayout, tablelayout</p></p>Linearlayout:<p><p>LinearLayout is called a linear layout, and as its name is, all controls in this layout are arranged in a linear direction.</p></p><p><p>LinearLayout the Orientation property to specify whether the arrangement direction is vertical or horizontal, if the value is vertical, the control is arranged vertically, and if the value is horizantal, the control is arranged horizontally</p></p><p><p>Code:</p></p><p><p>Activity_main.xml:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">xmlns:android</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "http://schemas.android.com/apk/res/android"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Xmlns:tools</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "http://schemas.android.com/tools"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Tools:context</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">=". Mainactivity "</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:orientation</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "vertical"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Button</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:text</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "BUTTON1"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:id</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@+id/button1"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Button</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:text</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "BUTTON2"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:id</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@+id/button2"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:orientation</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "horizontal"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Button</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:text</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "BUTTON3"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Button</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:text</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "BUTTON4"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p>The above code appears in the emulator as</p></p><p><p>  </p></p><p><p>  </p></p><p align="left"><p align="left">As we can see from the code above, the Android:orientation property controls the layout direction of the internal controls in the linear layout, vertical the vertical layout, the horizontal is the horizontal layout, The Android system default linear layout is horizontal (horizaontal).</p></p><p align="left"><p align="left">There is also a property android:gravity in the linear layout to control the gravity direction of the element in the layout, which has several parameters to choose From:</p></p><p align="left"><p align="left">center, bottom, center_horizontal, center_vertital, clip_horizontal, clip_vertital, end, fill, fill_horizontal, F ill_vertital, left, right, start, top.</p></p><p align="left"><p align="left">LinearLayout also has another important attribute--android:layout_weight, which allows us to specify the size of the control in a proportional way, which can play a very important role in the adaptation of the phone Screen.</p></p><p align="left"><p align="left">Code example:</p></p><p align="left"><p align="left">Activity_main.xml:</p></p><pre><span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">xmlns:android</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "http://schemas.android.com/apk/res/android"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Xmlns:tools</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "http://schemas.android.com/tools"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "match_parent"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Tools:context</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">=". Mainactivity "</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">android:orientation</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "horizontal"</span></span> <span style="color: #0000ff;"><span style="color: #0000ff;">></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">EditText</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "0dp"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:id</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@+id/usernameinput"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:text</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "please Enter your user name"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_weight</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "7"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span> <span style="color: #0000ff;"><span style="color: #0000ff;"><</span></span><span style="color: #800000;"><span style="color: #800000;">Button</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_width</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "0dp"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_height</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "wrap_content"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:id</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "@+id/loginbutton"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:text</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "login"</span></span><span style="color: #ff0000;"><span style="color: #ff0000;">Android:layout_weight</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">= "3"</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">/></span></span><span style="color: #0000ff;"><span style="color: #0000ff;"></</span></span><span style="color: #800000;"><span style="color: #800000;">LinearLayout</span></span><span style="color: #0000ff;"><span style="color: #0000ff;">></span></span></pre><p><p>The result of the Operation Is:</p></p><p><p>  </p></p><p><p>EditText the 7/10 button on the screen takes up 3/10 of the Screen. the principle is simple, the proportion of two controls is added to the total screen proportions, and then divided by their respective proportions</p></p><p><p></p></p><p><p></p></p><p><p>  </p></p><p><p>Android notes (vi) layout in android-linear layout</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.