The weight of Android LinearLayout Android:layout_weight

Source: Internet
Author: User
<span id="Label3"></p><p><p>The current EditText and button widgets just fit the size of their respective content, as shown in:</p></p><p><p></p></p><p><p>This is appropriate for the button, but not good for text boxes, because users may enter longer text Content. So it's better to be able to fill the entire screen width. LinearLayout uses the <em>weight</em> property to do this, you can use the Android:layout_weight property to set it.</p></p><p><p>The weight value refers to the amount of space that is occupied by each part, which is related to the amount of space the sibling part Occupies. It is similar to the composition of the beverage: "two vodka, one coffee liqueur", that is, the vodka in the wine accounted for two-thirds. For example, we set a view with a weight of 2 and another view with a weight of 1, then the total is 3, when the first view occupies 2/3 of the space and the second occupies 1/3 of the Space. If you add a third view and the weight is set to 1, then the first view (with a weight of 2) occupies 1/2 of the space, and the remaining two view accounts for 1/4. (note that the <span style="color: #ff0000;">premise of the use of weights is generally set to 0DP for the width or height of the view</span>, then the system calculates the space that the view should occupy according to the weight rules Above.) however, in many cases, if the view is set to the Match_parent attribute, then the above calculation of the weight is not usually proportional to, but the inverse ratio, that is, the weight of large but occupy a small space.</p></p><p><p>The default weight for all view is 0, and if only one view is set to a weight greater than 0, The view will occupy all the remaining space to remove the space occupied by the other view itself. The weight of the edittext is therefore set to 1 so that it can occupy all the space except for the Button.</p></p><p><p></p></p><p><p>After setting the weight</p></p><p><p></p></p><p><p>Now look at the full layout file contents:</p></p><p><p>Res/layout/activity_my.xml</p></p><pre><code class="lang-xml"><span class="hljs-pi"><?xml version= "1.0" encoding= "utf-8"?><span class="hljs-tag"><<span class="hljs-title">LinearLayout<span class="hljs-attribute">Xmlns:android=<span class="hljs-value">"http://schemas.android.com/apk/res/android"<span class="hljs-attribute">xmlns:tools=<span class="hljs-value">"http://schemas.android.com/tools"<span class="hljs-attribute">Android:layout_width=<span class="hljs-value">"match_parent"<span class="hljs-attribute">android:layout_height=<span class="hljs-value">"match_parent"<span class="hljs-attribute">android:orientation=<span class="hljs-value">"horizontal" ><span class="hljs-tag"><<span class="hljs-title">EditText<span class="hljs-attribute"><span class="hljs-attribute">android:id=<span class="hljs-value"> "@+id/edit_message" <span class="hljs-attribute">android:layout_weight=<span class="hljs-value"> "1" <span class="hljs-attribute"> Android:layout_width=<span class="hljs-value"> "0dp" <span class="hljs-attribute">android:layout_height=<span class="hljs-value"> "wrap_content" <span class="hljs-attribute">android:hint=<span class="hljs-value"> "@string/ Edit_message "/> <span class=" hljs-tag "><<span class=" hljs-title ">button <span class=" hljs-attribute "> Android:layout_width=<span class="hljs-value"> "wrap_content" <span class="hljs-attribute">android:layout_height =<span class="hljs-value"> "wrap_content" <span class="hljs-attribute">android:text=<span class="hljs-value"> "@ String/button_send "/><span class=" hljs-tag "></<span class=" hljs-title ">linearlayout> </span> </span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span> </span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></span></code></pre><p><p> Weight Android:layout_weight in Android LinearLayout </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.