Layout_weight attribute in linearlayout

Source: Internet
Author: User

Layout_weight attribute in linearlayout

Layout linearlayout code

XML Code
  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: Orientation = "vertical"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "fill_parent"
  6. >
  7. <Button
  8. Android: layout_width = "fill_parent"
  9. Android: layout_height = "fill_parent"
  10. Android: text = "button1"
  11. Android: Background = "#008000"
  12. Android: layout_weight = "0"
  13. />
  14. <Button
  15. Android: layout_width = "fill_parent"
  16. Android: layout_height = "fill_parent"
  17. Android: text = "button2"
  18. Android: Background = "# FFFF00"
  19. Android: layout_weight = "0"
  20. />
  21. </Linearlayout>
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: Orientation = "vertical" Android: layout_width = "fill_parent" Android: layout_height = "fill_parent"> <button Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: text = "button1" Android: Background = "#008000" Android: layout_weight = "0"/> <button Android: layout_width = "fill_parent" Android: layout_height = "fill_parent" Android: text = "button2" Android: Background = "# FFFF00" Android: layout_weight = "0"/> </linearlayout>

If the layout_weight attribute is not set, the default value is 0.

Button 1: button1, green

Button 2: button2, yellow

The following is a record of the changes in the weight attribute (layout_weight) of Button 1 and button 2.

The vertical layout Android: Orientation = "vertical" is tested here. Therefore, the Android: layout_height attribute of the component is used. If the horizontal layout is used, the Android: layout_width attribute of the component is used.

(1) Android: layout_height = "fill_parent"

Button1 (Weight = 0) button2 (Weight = 0) Button1 (Weight = 0) button2 (Weight = 1)

 

 

Button1 (Weight = 1) button2 (Weight = 1)

Button1 accounts for 1/2, while button2 accounts for 1/2

Button1 (Weight = 1) button2 (Weight = 2)

Button1 accounts for 2/3, while button2 accounts for 1/3


 

 

Button1 (Weight = 1) button2 (Weight = 9)

Button1 accounts for 9/10, while button2 accounts for 1/10

Button1 (Weight = 1) button2 (Weight = 30)

Button1 accounts for 30/31, while button2 accounts for 1/31

   

(2) Android: layout_height = "wrap_content"


 

 

Button1 (Weight = 0) button2 (Weight = 0)

Button1 (Weight = 0) button2 (Weight = 1)

 

 

Button1 (Weight = 1) button2 (Weight = 1)

Button1 accounts for 1/2, while button2 accounts for 1/2

Button1 (Weight = 1) button2 (Weight = 2)

Button1 accounts for 1/3, while button2 accounts for 2/3


 

 

Button1 (Weight = 1) button2 (Weight = 9)

 

Button1 (Weight = 1) button2 (Weight = 30)

 

   
   

The above experiments show that:

(1) Android: layout_height = "fill_parent"

(2) Android: layout_height = "wrap_content"

In the case of (1) and (2), the opposite is displayed under the same weight attribute.

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.