android开发笔记之权重(layout_weight)

我相信大家在布局文件中都用过权重(layout_weight)吧,当然这只有在线性布局(Linearlayout)中才有的,可是很多人也许都只是简单的理解为比。

其实权重就是:

把屏幕剩余空间按比例分配

大家先记住这句话,这里就来深入理解下权重,这里以水平排列为例(即宽度的权重),懂了水平的,竖直排列的(即高度的权重)自然同理。

①第一种情况(宽度为wrap_content):

a.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.weight.MainActivity" >
    <Button
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn1" />

    <Button
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn2" />
</LinearLayout>

效果:

现在两个按钮的宽度都为wrap_content,所以剩下的屏幕宽度为:

math_parent - 2*wrap_content,权重为1:1

则btn1所占宽度为:本身宽度 + 剩下屏幕的宽度的1/2 = wrap_content

+ 1/2(math_parent - 2*wrap_content)

btn2所占的宽度为:本身宽度 + 剩下屏幕的宽度的1/2 = wrap_content

+ 1/2(math_parent - 2*wrap_content)

从上面看到两式相等,所以两个按钮应该是平分整个屏幕宽度

b.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.weight.MainActivity" >
    <Button
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn1" />

    <Button
        android:layout_weight="2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn2" />
</LinearLayout>

效果:

可以看出,这两个按钮并不是1:2,那是为什么呢,我们来算下,

现在两个按钮的宽度都为wrap_content,所以剩下的屏幕宽度为:

math_parent - 2*wrap_content,权重为1:2

则btn1所占宽度为:本身宽度 + 剩下屏幕的宽度的1/3 = wrap_content

+ 1/3(math_parent - 2*wrap_content)

btn2所占的宽度为:本身宽度 + 剩下屏幕的宽度的2/3 = wrap_content

+ 2/3(math_parent - 2*wrap_content)

看这两个式子好像不能一眼看不出来,我们来个带个数,假设wrap_content = 120dp,math_parent = 900dp

那么btn1所占宽度为:340dp

btn2所占宽度为:560dp

现在知道为什么不是1:2了吧

c.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.weight.MainActivity" >
    <Button
        android:layout_weight="1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn1" />

    <Button
        android:layout_weight="2000"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="btn2" />
</LinearLayout>

效果:

大家可以看到,btn2的权重已经设的很大了,btn1的权重为1,但是btn1仍然能够显示。

继续来算下,现在两个按钮的宽度都为wrap_content,所以剩下的屏幕宽度为:

math_parent - 2*wrap_content,权重为1:2000

则btn1所占宽度为:本身宽度 + 剩下屏幕的宽度的1/2001 = wrap_content

+ 1/2001(math_parent - 2*wrap_content)

btn2所占的宽度为:本身宽度 + 剩下屏幕的宽度的2000/2001 = wrap_content

+ 2000/2001 (math_parent - 2*wrap_content)

现在可以知道为什么权重不管多大,都不能占满全屏的原因了吧。

②第二种情况(宽度为math_parent):

a.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.weight.MainActivity" >
    <Button
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="btn1" />

    <Button
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="btn2" />
</LinearLayout>

效果:

现在两个按钮的宽度都为match_parent,所以剩下的屏幕宽度为:

math_parent - 2*match_parent = -match_parent,权重为1:1

则btn1所占宽度为:本身宽度 + 剩下屏幕的宽度的1/2 = match_parent

+ 1/2(math_parent - 2*match_parent) = 1/2match_parent

btn2所占的宽度为:本身宽度 + 剩下屏幕的宽度的1/2 = match_parent

+ 1/2(math_parent - 2*match_parent) = 1/2match_parent

从上面看到两式相等,所以两个按钮应该是平分整个屏幕宽度

b.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.weight.MainActivity" >
    <Button
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="btn1" />

    <Button
        android:layout_weight="2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="btn2" />
</LinearLayout>

效果:

大家可以看到一个很奇怪的现象,btn2的权重更大,但是它反而比btn1占的宽度更少了,为什么呢,算一算。

现在两个按钮的宽度都为match_parent,所以剩下的屏幕宽度为:

math_parent - 2*match_parent = -match_parent,权重为1:2

则btn1所占宽度为:本身宽度 + 剩下屏幕的宽度的1/3 = match_parent

+ 1/3(math_parent - 2*match_parent) = 2/3match_parent

btn2所占的宽度为:本身宽度 + 剩下屏幕的宽度的2/3 = match_parent

+ 2/3(math_parent - 2*match_parent) = 1/3match_parent

现在知道为什么了吧。

c.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.weight.MainActivity" >
    <Button
        android:layout_weight="1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="btn1" />

    <Button
        android:layout_weight="2000"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="btn2" />
</LinearLayout>

效果:

为什么只看到btn1呢

现在两个按钮的宽度都为match_parent,所以剩下的屏幕宽度为:

math_parent - 2*match_parent = -match_parent,权重为1:2000

则btn1所占宽度为:本身宽度 + 剩下屏幕的宽度的1/2001 = match_parent

+ 1/2001(math_parent - 2*match_parent) = 2000/2001match_parent

btn2所占的宽度为:本身宽度 + 剩下屏幕的宽度的2000/2001= match_parent

+ 2000/2001(math_parent - 2*match_parent) = 1/2001match_parent

这样可以看出btn2的宽度基本可以忽略不计了。

③第三种情况(0dp):

通过上面的示例,大家都应该知道怎么算权重了吧,这里就演示下1:2的情况

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal"
    tools:context="com.example.weight.MainActivity" >
    <Button
        android:layout_weight="1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="btn1" />

    <Button
        android:layout_weight="2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="btn2" />
</LinearLayout>

效果:

现在两个按钮的宽度都为0dp,所以剩下的屏幕宽度为:

math_parent - 2*0 = match_parent,权重为1:2

则btn1所占宽度为:本身宽度 + 剩下屏幕的宽度的1/3 =

0 + 1/3(math_parent - 2*0) = 1/3match_parent

btn2所占的宽度为:本身宽度 + 剩下屏幕的宽度的2/3 =

0+ 2/3(math_parent - 2*0) = 2/3match_parent

可以看出btn1和btn2实际的宽度比就等于它们的权重比。

总结:

①权重是把屏幕剩余空间按比例分配

②如果wrap_content,那么权重越大,位置占的越多,再小小不过wrap_content

③如果match_parent,那么权重越大,位置占的越少,再大大不过match_parent

④如果使用0dp,则实际的宽度比就等于权重比

时间: 2024-08-24 11:45:38

android开发笔记之权重(layout_weight)的相关文章

[置顶] Android开发笔记(成长轨迹)

分类: 开发学习笔记2013-06-21 09:44 26043人阅读 评论(5) 收藏 Android开发笔记 1.控制台输出:called unimplemented OpenGL ES API 调用了未实现的OpenGL ES API函数,一般由于导入的第三方库如地图库,里面有用到OpenGL,但是模拟器的硬件默认是没有这个的,所以需要我们编辑模拟器Emulation Options选项勾选 Use Host GPU 然后重启模拟器再尝试,如果还是这个错误,那么我们只好用真机测试了. 2.

Android开发笔记(一百二十)两种侧滑布局

SlidingPaneLayout SlidingPaneLayout是Android在android-support-v4.jar中推出的一个可滑动面板的布局,在前面<Android开发笔记(一百零一)滑出式菜单>中,我们提到水平布局时的LinearLayout无法自动左右拉伸,必须借助于手势事件才能拉出左侧隐藏的布局,现在SlidingPaneLayout便是为了解决LinearLayout无法自动拉伸的缺陷.只要我们在布局文件的SlidingPaneLayout节点下定义两个子布局,那么

Android开发笔记(一百零三)地图与定位SDK

集成地图SDK 国内常用的地图SDK就是百度和高德了,二者的用法大同小异,可按照官网上的开发指南一步步来.下面是我在集成地图SDK时遇到的问题说明: 1.点击基本地图功能选项,不能打开地图,弹出"key验证出错!请在AndroidManifest.xml文件中检查key设置的"的红色字提示.查看日志提示"galaxy lib host missing meta-data,make sure you know the right way to integrate galaxy&

[APP] Android 开发笔记 003

接上节 [APP] Android 开发笔记 002 5. 使用ant release 打包 1)制作 密钥文件 release.keystore (*.keystore) keytool -genkey -v -keystore "release.keystore" -alias "release" -keyalg "RSA" -validity "10000" 这里需要注意的是: -keystore "relea

《ArcGIS Runtime SDK for Android开发笔记》——(10)、ArcGIS Runtime SDK支持的空间数据类型

1.前言 移动端的数据来源非常重要,它决定了移动端功能的实现.早期的ArcGIS Android API中,主要以接入在线的数据源为主,因此主要实现在线的地图浏览.查询和路径分析.地理处理等从操作:在v1.0.1版本中,ArcGIS移动产品第一次可以加载松散型切片,自此逐渐掀开了对本地离线数据源的支持,也因此可以在移动端实现越来越受欢迎的离线功能.现在最新的10.2.7 API离线支持数据主要包括紧凑型切片.tpk切片包..geodatabase..shp文件.地名地址库.网络数据集. 转载请注

Android开发笔记--hello world 和目录结构

原文:Android开发笔记--hello world 和目录结构 每接触一个新东西 都有一个hello world的例子. 1.新建项目 2.配置AVD AVD 没有要新建个,如果不能创建 运行SDK Manager更新 3.接着运行就可以了 第一次启动要1分多钟 不要关 4.添加代码 5.接着在运行就OK了 目录结构 1.src - 用于放置源程序 2.gen - 自动生成 R.java 文件,用于引用资源文件(即 res 目录下的数据) 3.assets - 用于放置原始文件,Androi

Android开发笔记(一百一十六)网络学习资源

知名网站 本系列的开发笔记,对Android开发来说只是沧海一瓢,还有更多的技术等待我们去汲取.下面列出几个常用的开发网站,供初学者上路: 首先当然是国内首屈一指的技术网站csdn啦,csdn提供了众多频道,包括博客.论坛.下载.问答等等,其中博客专栏提供了最新的技术文章,值得推荐.csdn博客专栏的地址是 http://blog.csdn.net/column.html 下面是csdn博客专栏的网页截图: 其次是国外有名的开源网站GitHub,这里有众多的开源项目源码,是开发者分享代码的乐园.

Android开发笔记(八十八)同步与加锁

同步synchronized 同步方法 synchronized可用来给方法或者代码块加锁,当它修饰一个方法或者一个代码块的时候,同一时刻最多只有一个线程执行这段代码.这就意味着,当两个并发线程同时访问synchronized代码块时,两个线程只能是排队做串行处理,另一个线程要等待前一个线程执行完该代码块后,才能再次执行synchronized代码块. 使用synchronized修饰某个方法,该方法便成为一个同步方法,在同一时刻只能有一个线程执行该方法.可是,synchronized的锁机制太

android开发笔记1

1.强制横屏,不能转屏 在AndroidManifest中需要的activity里: <application android:icon="@drawable/ic_launcher" android:label="@string/app_name">   <activity android:name=".GestureFlip"     android:label="@string/app_name"