Android Layout_weight Usage Summary

Source: Internet
Author: User

Today in the use of androidlayout_weight encountered some strange problem, on the Internet to check a bit, found this article is very detailed, turned over, thank you to share, write very detailed.

LinearLayout is very common in Android development, and the android:layout_weight of LinearLayout's internal controls is important in some scenarios, such as the need to scale. Android does not provide a control such as table, although there is a tablelayout, but it is not as we imagine the HTML inside the table so useful, we often use the ListView to achieve table effect, but the column alignment is more troublesome, Now with LinearLayout and attribute android:layout_weight can be well solved. Now let's experience the next Layout_weight property together.first, the layout_width of the control within the LinearLayout is set to "Wrap_content", look at the XML configuration:<linearlayout
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "1"
>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
android:layout_weight= "1"
Android:background= "#aa0000"
android:gravity= "Center"
android:text= "1"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
Android:layout_weight= "2"
Android:background= "#00aa00"
android:gravity= "Center"
android:text= "1"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
android:layout_weight= "3"
Android:background= "#0000aa"
android:gravity= "Center"
android:text= "1"/>
</LinearLayout>:You can see that these three textview are displayed in proportion to the 1:2:3, so it seems to be possible to achieve a proportional display, but there is a problem, if the length of text within the TextView together with the longer text TextView will increase the width, see the following configuration and effect: <linearlayout
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "1" >
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
android:layout_weight= "1"
Android:background= "#aa0000"
android:gravity= "Center"
android:text= "1111111111111111111111111111111111111111111"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
Android:layout_weight= "2"
Android:background= "#00aa00"
android:gravity= "Center"
android:text= "2"/>
<textview
Android:layout_width= "Wrap_content"
android:layout_height= "Fill_parent"
android:layout_weight= "3"
Android:background= "#0000aa"
android:gravity= "Center"
android:text= "3"/>
</LinearLayout> effect: So it seems that we need to scale and can not be achieved, through the world Google finally found the solution is to set the Layout_width set to "Wrap_content". Configuration and Effect see below: <linearlayout
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "1" >
<textview
Android:layout_width= "0DP"
android:layout_height= "Fill_parent"
android:layout_weight= "1"
Android:background= "#aa0000"
android:gravity= "Center"
android:text= "1111111111111111111111111111111111111111111"/>
<textview
Android:layout_width= "0DP"
android:layout_height= "Fill_parent"
Android:layout_weight= "2"
Android:background= "#00aa00"
android:gravity= "Center"
android:text= "2"/>
<textview
Android:layout_width= "0DP"
android:layout_height= "Fill_parent"
android:layout_weight= "3"
Android:background= "#0000aa"
android:gravity= "Center"
android:text= "3"/>
</LinearLayout> effect: This finally achieves our proportional display effect, the feeling is very strange, the Android development framework of the big guys sometimes the design is really a little weird.Second, the layout_width of the control within the LinearLayout is set to "Fill_parent", take a look at the XML configuration:<linearlayout
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "1" >
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "1"
Android:background= "#aa0000"
android:gravity= "Center"
android:text= "1"/>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:layout_weight= "2"
Android:background= "#00aa00"
android:gravity= "Center"
android:text= "2"/>
The </LinearLayout> effect is as follows: oddly enough, the whole width is divided into 3 pieces, the first textview accounted for two pieces, so it appears that the lower the weight value the greater the priority. Only two textview seem to see some truth, so let's see what the three effects are: <linearlayout
android:orientation= "Horizontal"
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "1" >
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "1"
Android:background= "#aa0000"
android:gravity= "Center"
android:text= "1"/>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
Android:layout_weight= "2"
Android:background= "#00aa00"
android:gravity= "Center"
android:text= "2"/>
<textview
Android:layout_width= "Fill_parent"
android:layout_height= "Fill_parent"
android:layout_weight= "3"
Android:background= "#0000aa"
android:gravity= "Center"
android:text= "3"/>
</LinearLayout> effect: What do you mean? The third TextView lost, it is very strange, let us try again, the weight to 2,3,4 to see the effect: This effect is very confusing, I have been trying to find out an exact proportion formula, but so far not found. If any of the great gods can handle it, forget to enlighten me.Although this android:layout_weight attribute is very strange, fortunately we have achieved the goal:

Displays each child control in the LinearLayout proportionally, set android:layout_width= "0DP" If the setting is Android:layout_height= "0DP" in the vertical direction. In this case, the proportion of a child control occupying LinearLayout is the weight of the value/weight value of all controls within the linearlayout of this control.

http://hunankeda110.iteye.com/blog/1879410

Android Layout_weight Usage Summary

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.