1.1 Pooling Weightsum Properties and Layout_weight properties

Source: Internet
Author: User

the 1th chapter, "Building high-quality Android apps: 50 tips for Android Development", describes some tips and recommendations for Android layout. In this chapter, readers can learn not only how to create a specific type of layout from scratch, but also how to improve and optimize existing layouts. This section introduces the shared Weightsum property and the Layout_weight property.

AD:2014WOT Global Software Technology Summit Beijing Station course video release

1th chapter on the utilization of layout

This chapter describes some tips and recommendations for Android layout. In this chapter, readers can learn not only how to create a specific type of layout from scratch, but also how to improve and optimize existing layouts.

Hack 1 Using the weight property to achieve a centered display of the view

Android v1.6+

As I explain how to create a view from an XML file when I talk to a developer, a developer asks, "What should I do if I want to center the button and occupy half the width of its parent view?" "At first, I didn't fully understand what he meant, and then he painted the function he wanted to achieve on the blackboard. The features he wants to implement are shown in 1-1 and figure 1-2.

It looks pretty simple, doesn't it? Now, let the reader implement this function in 5 minutes. In this hack, we analyze how to solve this problem by combining the Android:weightsum property of LinearLayout with the Android:layout_weight attribute of LinearLayout's child view. It may sound simple, but I often ask this question in interviews, and few interviewers know the best answer.

1.1 Pooling Weightsum Properties and Layout_weight properties

The sizes of different Android devices are often different. As a developer, we need to create an XML file for screens of different sizes. Hard coding is undesirable, so other methods are needed to organize the view.

This section analyzes how to share the Layout_weight and Weightsum properties to fill any remaining space inside the layout. A description of the development documentation for ANDROID:WEIGHTSUM (see section 1.3) is similar to what we now want to implement, with the following documentation:

"Defines the maximum value for the sum of weight. If this value is not specified, the cumulative value of the Layout_weight property of all child views is used as the maximum value of the sum. A typical case is: by specifying the Layout_weight property of the child view to 0.5, and setting the LinearLayout's Weightsum property to 1.0, the implementation child view occupies 50% of the available width. ”

Imagine a scenario where we want to place other objects in the box. The ratio of free space in the box is weightsum, and the proportion of free space in the box is layout_weight. For example, the weightsum of a box is 1, and we need to place two objects in the box: Object A and Object B. The layout_weight of object A is 0.25, and the Layout_weight of object B is 0.75. So, object A can occupy 25% of the space in the box, and object B can occupy the remaining 75% of the space.

The solution to the problem discussed at the beginning of this chapter is similar. We specify a weightsum for the parent view, and then specify that the button's Android:layout_weight property is half of Weightsum. The source code for the XML file is as follows:

The Android:weightsum attribute value of the inearlayout is 1, which indicates that the sum of the weight proportions of all the child views within it is 1. LinearLayout only one child view: Button control. The Android:layout_width attribute value for ˙utton is 0DP, so the width of the button needs to be determined according to the Android:weight-sum attribute. The Android:layout_weight attribute value for the ˙utton is 0.5, and the final button will occupy 50% of the free space.

This is followed by an example of a linear-layout with a width of 200dp,android:weightsum attribute value of 1. The formula for calculating the button width is as follows:

Because the specified button has a width of 0dp,button of weight of 0.5,sum (weight) equal to 1, the result is as follows:

1.1 Pooling Weightsum Properties and Layout_weight properties

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.