Android 程序 LinearLayout布局 参数layout_weight 探讨
官方参考文档 对LinearLayout.LayoutParams中的android:layout_weight解释如下:
Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams.
Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
extra space: 是指各个子view占用完 长度或宽度 之后再来分配。
下面引用两篇文章,仔细看完会有一定了解。
第一篇: The use of layout_weight with Android layouts
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 of View
s can be ordered and one of the ways to do that is through the use of the LayoutParam
layout_weight
. This is an often overlooked, extremely powerful, but also tricky feature of layouts.
The Android developers website (developer.android.com) defines layout_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:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#0000FF"
android:layout_height="match_parent"
android:layout_width="match_parent" />
<LinearLayout
android:background="#00FF00"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
Not using layout_weight (不写layout_weight的话,第一个子元素的match_parent会占满整个父元素,第二个就没有空间了)
------------------------------------------
Of course the split does not work because the layout_height
of the first LinearLayout
is set to match_parent causing it to take up all available space and leaving no room for the secondLinearLayout
. Changing the layout_height
to wrap_content
will not help because the LinearLayout
s have 0 heights making them invisible.
At this point we can show what layout_weight
can do. Have a look at this, changed, piece of code and the resulting screenshot below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#0000FF"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1" />
<LinearLayout
android:background="#00FF00"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1" />
</LinearLayout>
Using layout_weight
----------------------------------------------
By setting a layout_weight
for the two inner LinearLayouts
we can tell the parent layout to divide the available space between its children. In this example, we have set the two layout_weight
values of the child layouts to the same value, and they will be given an equal part of the available space.
Setting a layout_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 parent View
, according to the value of each View
‘s layout_weight
and its ratio to the overall layout_weight
specified in the current layout for this and other View
elements.
To give an example: in the above example we have twoLinearLayouts
. If the layout_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 a layout_weight
of 1 and the second has a layout_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.
Unequal division of weight
----------------------------------
The divide is one third and two third but, still not exactly what we want. Take a close look at the code. We want the firstLinearLayout
to occupy two third of the screen. Its layout_weight
is set to 2. What we see is that it only occupies one third of the screen. This is the tricky bit of using layout_weight
.
The problems are the circumstances it is used in. In this case it is used with layout_height
set to match_parent
. In some (!) cases this keeps Android from interpreting the layout_weight
s correctly. In this case, a vertical layout of view elements, the solution is setting layout_height
s to 0dp
. With a horizontal layout of view elements layout_width
should be set to 0dp
.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#0000FF"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="2" />
<LinearLayout
android:background="#00FF00"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1" />
</LinearLayout>
Correct division of weight (如果希望按比例分配空间的话, 把android:layout_height(纵向) 或者 android:layout_width(横向) 设置成0dp。)
-------------------------------------
In the above examples the total weight of a View
element is calculated by adding all the weights of its children. This can be overridden by adding a weightSum
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 parent LinearLayout
will take the rest (the remaining one fourth):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FF0000"
android:weightSum="4"
android:padding="5dp"> <!-- to show what the parent is -->
<LinearLayout
android:background="#0000FF"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="2" />
<LinearLayout
android:background="#00FF00"
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1" />
</LinearLayout>
Example with weightSum (不占满父元素,那就在父元素上使用 android:weightSum)
----------------------------------------
As a conclusion let’s have another look at a potential gotcha when using layout_weight
s. First switch to a horizontalLinearLayout
. This contains two TextView
s, each with alayout_width
set to 1, but with text of very different lengths in each:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:text="small"
android:layout_width="wrap_content"
android:layout_weight="1" />
<TextView
android:layout_height="wrap_content"
android:text="A very very long text that needs to wrap."
android:layout_width="wrap_content"
android:layout_weight="1" />
</LinearLayout>
Texts not shown as wanted
---------------------------------------
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
forlayout_width
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_height="wrap_content"
android:text="small"
android:layout_width="0dp"
android:layout_weight="1" />
<TextView
android:layout_height="wrap_content"
android:text="A very very long text that needs to wrap."
android:layout_width="0dp"
android:layout_weight="1" />
</LinearLayout>
=============================================
第二篇文章: android中layout_weight的理解 文章从代码角度解释了一下分配原则, 具体内容点击链接就能看。
Android 程序 LinearLayout布局 参数layout_weight 探讨的更多相关文章
- Android:LinearLayout布局中Layout_weight的深刻理解
首先看一下LinearLayout布局中Layout_weight属性的作用:它是用来分配属于空间的一个属性,你可以设置他的权重.很多人不知道剩余空间是个什么概念,下面我先来说说剩余空间. 看下面代码 ...
- Android学习——LinearLayout布局实现居中、左对齐、右对齐
android:orientation="vertical"表示该布局下的元素垂直排列: 在整体垂直排列的基础上想要实现内部水平排列,则在整体LinearLayout布局下再创建一 ...
- 15.Android中LinearLayout布局一些小记录
在App中,我们经常看到布局中会有分割线,直接上代码: <?xml version="1.0" encoding="utf-8"?> <Lin ...
- Android之LinearLayout布局下怎么让按钮固定在底部
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...
- app开发历程————Android程序解析服务器端的JSON格式数据,显示在界面上
上一篇文章写的是服务器端利用Servlet 返回JSON字符串,本文主要是利用android客户端访问服务器端链接,解析JSON格式数据,放到相应的位置上. 首先,android程序的布局文件main ...
- Android 布局学习之——Layout(布局)详解二(常见布局和布局参数)
[Android布局学习系列] 1.Android 布局学习之——Layout(布局)详解一 2.Android 布局学习之——Layout(布局)详解二(常见布局和布局参数) 3.And ...
- 将Android Studio默认布局ConstraintLayout切换成LinearLayout
将Android Studio默认布局ConstraintLayout切换成LinearLayout 大部分人初次使用google android 扁平化布局ConstraintLayout都 ...
- 无废话Android之常见adb指令、电话拨号器、点击事件的4种写法、短信发送器、Android 中各种布局(1)
1.Android是什么 手机设备的软件栈,包括一个完整的操作系统.中间件.关键的应用程序,底层是linux内核,安全管理.内存管理.进程管理.电源管理.硬件驱动 2.Dalvik VM 和 JVM ...
- [置顶] Android系统五大布局详解Layout
我们知道Android系统应用程序一般是由多个Activity组成,而这些Activity以视图的形式展现在我们面前,视图都是由一个一个的组件构成的.组件就是我们常见的Button.TextEdit等 ...
随机推荐
- Android篇---Styles和Themes常见用法可能疑点小结
1.style和theme的区别: 简而言之,style指的就是安卓中一个UI控件的样式,而themes指的是安卓中一个activity界面或者整个安卓应用整体的样式.theme的范围比style的范 ...
- 扒一扒IT大佬高考:马云数学1分考北大 李彦宏是状元
http://news.cnblogs.com/n/522622/ 高考今天正式拉开序幕,而像李彦宏.马云等 IT 大佬之前也都参加过高考,他们成绩又都是怎样的呢? 马化腾:放弃天文梦选择计算机 20 ...
- 运行JavaWeb项目报错Access denied for user 'root'@'localhost' (using password: YES)
问题重现:(以下讨论范围仅限Windows环境): C:\AppServ\MySQL> mysql -u root -p Enter password: ERROR 1045 (28000): ...
- POJ 2411 Mondriaan's Dream (状压DP,骨牌覆盖,经典)
题意: 用一个2*1的骨牌来覆盖一个n*m的矩形,问有多少种方案?(1<=n,m<=11) 思路: 很经典的题目,如果n和m都是奇数,那么答案为0.同uva11270这道题. 只需要m个b ...
- SQL_关联映射
关联映射:一对多/多对一 存在最普遍的映射关系,简单来讲就如球员与球队的关系: 一对多:从球队角度来说一个球队拥有多个球员 即为一对多 多对一:从球员角度来说多个球员属于一个球队 即为多对一 数据表间 ...
- Codeforces Round #319 (Div. 2) C Vasya and Petya's Game (数论)
因为所有整数都能被唯一分解,p1^a1*p2^a2*...*pi^ai,而一次询问的数可以分解为p1^a1k*p2^a2k*...*pi^aik,这次询问会把所有a1>=a1k &&am ...
- UVA 1471 Defense Lines 防线 (LIS变形)
给一个长度为n的序列,要求删除一个连续子序列,使剩下的序列有一个长度最大的连续递增子序列. 最简单的想法是枚举起点j和终点i,然后数一数,分别向前或向后能延伸的最长长度,记为g(i)和f(i).可以先 ...
- python_92_面向对象初体验
class dog: def __init__(self,name): self.name=name def bulk(self): print('%s汪汪汪!'%self.name) d1=dog( ...
- Android layout的XML
[注]此文是在学习andriod中的一些理解和总结,若有错望留言指教,谢谢 1 <RelativeLayout xmlns:android="http://schemas.androi ...
- tcp 高性能服务, netty,mqtt
1. io 线程不要有比较长的服务. 全部异步化. [1] netty 权威指南上只是说业务复杂时派发到业务线程池种. 共用的线程池最好都轻量. 多层线程池后, 下层的可以进行隔离. 这个是 mqtt ...