Talking about Layout_weight in Android

Source: Internet
Author: User

Introduction

During the development of Android, we often need to lay out the interface proportionally, and we will generally use the Layout_ property to set it. Today's article we will briefly introduce the use and layout of the layout_weight principle. As the project experience accumulates, the blog post may be updated at all times.

use layout_weight under LinearLayout

Today, let's introduce the problem of using layout_weight under LinearLayout. Let's say we need to implement something like this:

Let's look at the layout style settings at this time:

1 <!--layout When adjusting a horizontal screen -2     <LinearLayout3         android:orientation= "Horizontal"4 Android:layout_width= "Match_parent"5 Android:layout_height= "Wrap_content"6 Android:layout_marginleft= "16DP"7 Android:layout_marginright= "16DP">8 9         <ButtonTen             Android:layout_width= "Wrap_content" One Android:layout_height= "Wrap_content" A Android:id= "@+id/crime_date" - Android:layout_weight= "1"/> -  the         <CheckBox -             Android:layout_width= "Wrap_content" - Android:layout_height= "Wrap_content" - Android:text= "@string/crime_solved_label" + Android:id= "@+id/crime_solved" - Android:layout_weight= "1"/> +     </LinearLayout>

We see in the layout style file that we see the button layout_weight is the layout_weight of 1,checkbox is 1. What does that mean? Let's take a detailed introduction below.

The Layout_weight property tells LinearLayout how to arrange the sub-component layout. We see that the Layout_weight value of the button is the same as the layout_weight value of the checkbox, but it does not mean that they occupy the same width on the screen. In fact, when calculating the width of a subassembly, LinearLayout uses a mixed value of Layout_width layout_weight. let's look at the detailed steps:

  First step : LinearLayout View the value of the Layout_width property (the vertical orientation is the value of the Layout_height property). The Layout_width property values for the button and checkbox components are set to wrap_content, so they get only enough space to draw themselves.

  

  Step two : LinearLayout the allocation of additional space based on the value of the Layout_weight attribute. :

  

In a layout, the button and checkbox components have the same Layout_weight property values, so they share the same size of extra space. If you set the weight value of the button component to 2, it will get 2/3 extra space and the checkbox component gets the remaining 1/3. Let's take a look at the effect:

  

  What do you do if you want LinearLayout to assign exactly the same width to the respective view? It is simple to set the Layout_width property value of each component to 0DP to avoid the space allocation of the first step, so that LinearLayout will only consider using the Layout_weight property value to complete the required space allocation.

  :

  

Talking about Layout_weight in Android

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.