android layout_weight详解
The popular Android OS uses layouts to display
View
s on the screen. A View
is a widget that has an appearance on the screen. Examples of widgets are radio buttons, labels, edit boxes, etc. The appearance and sequence ofView
s can be ordered and one of the ways to do that is through the use of theLayoutParam
layout_weight
. This is an often overlooked, extremely powerful, but also tricky feature of layouts.
The Android developers website (developer.android.com) defineslayout_weight
as follows:
Indicates how much of the extra space in the [ViewGroup] will be allocated to the view associated with these LayoutParams.
This definition does not help us very much, especially because it is only true under specific circumstances as we see below. Let’s have a look at some examples. In the first example we want to split the display in half vertically with the following layout:
<br /><?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="match_parent"<br /> android:layout_height="match_parent"><br /> <LinearLayout<br /> android:background="#0000FF"<br /> android:layout_height="match_parent"<br /> android:layout_width="match_parent" /><br /> <LinearLayout<br /> android:background="#00FF00"<br /> android:layout_height="match_parent"<br /> android:layout_width="match_parent" /><br />
Of course the split does not work because thelayout_height
of the firstLinearLayout
is set to match_parent causing it to take up all available space and leaving no room for the secondLinearLayout
. Changing thelayout_height
towrap_content
will not help because theLinearLayout
s have 0 heights making them invisible.
At this point we can show whatlayout_weight
can do. Have a look at this, changed, piece of code and the resulting screenshot below:
<br /><?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="match_parent"<br /> android:layout_height="match_parent"><br /> <LinearLayout<br /> android:background="#0000FF"<br /> android:layout_height="match_parent"<br /> android:layout_width="match_parent"<br /> android:layout_weight="1" /><br /> <LinearLayout<br /> android:background="#00FF00"<br /> android:layout_height="match_parent"<br /> android:layout_width="match_parent"<br /> android:layout_weight="1" /><br /></LinearLayout><br />
By setting alayout_weight
for the two innerLinearLayouts
we can tell the parent layout to divide the available space between its children. In this example, we have set the twolayout_weight
values of the child layouts to the same value, and they will be given an equal part of the available space.
Setting alayout_weight
means that the default value of this attribute is changed from 0. Assigning a value higher than zero will split up the rest of the available space in the parentView
, according to the value of eachView
‘slayout_weight
and its ratio to the overalllayout_weight
specified in the current layout for this and otherView
elements.
To give an example: in the above example we have twoLinearLayouts
. If thelayout_weight
of each of the twoLinearLayouts
is set to 1, the remaining width in the parent layout will be split equally between them (as we have seen above). If the first one has alayout_weight
of 1 and the second has alayout_weight
of 2, then the total weight is three and one third of the remaining space will be given to the first, and two thirds to the second, see the screenshot below.
The divide is one third and two third but,stillnot exactly what we want. Take a close look at the code. We want the firstLinearLayout
to occupy two third of the screen. Itslayout_weight
is set to 2. What we see is that it only occupies one third of the screen. This is the tricky bit of usinglayout_weight
.
The problems are the circumstances it is used in. In this case it is used withlayout_height
set tomatch_parent
. In some (!) cases this keeps Android from interpreting thelayout_weight
s correctly. In this case, a vertical layout of view elements, the solution is settinglayout_height
s to0dp
. With a horizontal layout of view elementslayout_width
should be set to0dp
.
<br /><?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="match_parent"<br /> android:layout_height="match_parent"><br /> <LinearLayout<br /> android:background="#0000FF"<br /> android:layout_height="0dp"<br /> android:layout_width="match_parent"<br /> android:layout_weight="2" /><br /> <LinearLayout<br /> android:background="#00FF00"<br /> android:layout_height="0dp"<br /> android:layout_width="match_parent"<br /> android:layout_weight="1" /><br /></LinearLayout><br />
In the above examples the total weight of aView
element is calculated by adding all the weights of its children. This can be overridden by adding aweightSum
to the parent layout. This provides us with even more control over things. The childrenLinearLayout
s can be specified to take their respective screen parts (two fourth and one fourth) and the parentLinearLayout
will take the rest (the remaining one fourth):
<br /><?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="vertical"<br /> android:layout_width="match_parent"<br /> android:layout_height="match_parent"<br /> android:background="#FF0000"<br /> android:weightSum="4"<br /> android:padding="5dp"> <!-- to show what the parent is --><br /> <LinearLayout<br /> android:background="#0000FF"<br /> android:layout_height="0dp"<br /> android:layout_width="match_parent"<br /> android:layout_weight="2" /><br /> <LinearLayout<br /> android:background="#00FF00"<br /> android:layout_height="0dp"<br /> android:layout_width="match_parent"<br /> android:layout_weight="1" /><br /></LinearLayout><br />
As a conclusion let’s have another look at a potential gotcha when usinglayout_weight
s. First switch to a horizontalLinearLayout
. This contains twoTextView
s, each with alayout_width
set to 1, but with text of very different lengths in each:
<br /><?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="horizontal"<br /> android:layout_width="match_parent"<br /> android:layout_height="match_parent"><br /> <TextView<br /> android:layout_height="wrap_content"<br /> android:text="small"<br /> android:layout_width="wrap_content"<br /> android:layout_weight="1" /><br /> <TextView<br /> android:layout_height="wrap_content"<br /> android:text="A very very long text that needs to wrap."<br /> android:layout_width="wrap_content"<br /> android:layout_weight="1" /><br /></LinearLayout><br />
As with the vertical layout the result is not what we expect. This time because of the specified
layout_width
. When calculating the layout, Android calculates the width of the two text controls first and the remaining space is then divided between them equally. Because the second TextView
is wider, due to its longer text, it appears to be taking up most of the space. As seen earlier the solution is simple, using 0dp
for layout_width
: <br /><?xml version="1.0" encoding="utf-8"?><br /><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"<br /> android:orientation="horizontal"<br /> android:layout_width="match_parent"<br /> android:layout_height="match_parent"><br /> <TextView<br /> android:layout_height="wrap_content"<br /> android:text="small"<br /> android:layout_width="0dp"<br /> android:layout_weight="1" /><br /> <TextView<br /> android:layout_height="wrap_content"<br /> android:text="A very very long text that needs to wrap."<br /> android:layout_width="0dp"<br /> android:layout_weight="1" /><br /></LinearLayout><br />
原文地址:http://www.chess-ix.com/2012/01/17/the-use-of-layout_weight-with-android-layouts/
官网地址:http://developer.android.com/guide/topics/ui/layout/linear.html
原文详细介绍了关于layout_weight的神奇魅力,本人喜欢原文就没有进行翻译,如果看了又不懂的可以互相讨论一下
更多相关文章
- [ 转载]Android(安卓)by example : MVVM +Data Binding -> Model
- android 根据短信地址匹配联系人姓名
- Android(安卓)Dev入门笔记
- JNI Examples for Android
- ch08 Android(安卓)Intent
- android 源码下载及问题
- Android(安卓)UI + Function
- Android中判断是否有声音在播放
- 【Android(安卓)应用开发】GitHub 优秀的 Android(安卓)开源项目