Getting Started with Android (v) Screen components 5.2 LinearLayout linear layouts

Source: Internet
Author: User
Tags constant xmlns

LinearLayout organizes views into one row or column. A child view can be arranged vertically or horizontally. To see how LinearLayout works, consider the typical mail.xml file first.

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=   

 
"http://schemas.android.com/apk/" Res/android " 
    android:layout_width=" fill_parent " 
    android:layout_height=" fill_parent " 
    android:o" rientation= "vertical" >    
       
    <textview 
        android:layout_width= "Fill_parent" 
        Wrap_content " 
        android:text=" @string/hello "/>    
       
</LinearLayout>

In Main.xnl, you can see that the root node element is <linearlayout>, and it contains a <TextView> element. This <LinearLayout> element controls the order of views that it contains.

Each view and viewgroup have some common attributes, some examples:

Property Describe
Layout_width Specify the width of view or ViewGroup
Layout_height Specify the height of view or ViewGroup
Layout_margintop Specify extra space above view or ViewGroup
Layout_marginbottom Specify additional space below view or ViewGroup
Layout_marginleft Specify extra space to the left of view or ViewGroup
Layout_marginright Specify additional space on the right side of view or ViewGroup
Layout_gravity Specify the placement of child views in view or ViewGroup
Layout_weight Specify additional space dimensions assigned to view or ViewGroup
Layout_x Specify the X coordinate of view or viewgroup
Layout_y Specify the y-coordinate of the view or ViewGroup

For example, the,<textview> element uses the Fill_parent constant, so the width fills the width of the entire layout. Its height is set to the Wrap_content constant, which means that its height will be the same as the layout it is in. If you don't want to <TextView> occupy the entire screen, you can set the Layoutt_width element to wrap_content, like this:

    <textview 
        android:layout_width= "wrap_content" 
        android:layout_height= "wrap_content" 
        android:text = "@string/hello"/>

The code above sets the width of the textview to the same length as the text it contains.

Please see the following layout:

<?xml version= "1.0" encoding= "Utf-8"?> <linearlayout xmlns:android=    
"http://schemas.android.com/apk/" Res/android " 
    android:layout_width=" fill_parent " 
    android:layout_height=" fill_parent " 
    android:o" rientation= "vertical" >    
       
    <textview    
        android:layout_width= "100DP" 
        android:layout_height= "WRAP_" Content " 
        android:text=" @string/hello "/>    
       
    <button    
        android:layout_width=" 160DP " 
        android: layout_height= "Wrap_content" 
        android:text= "button"/>    
       
</LinearLayout>

Here, the width of the TextView and button is set to a fixed value.

See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/OS/extra/

Related Article

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.