android: weight是线性布局的特有属性,控件的宽度和高度的不同,也会存在差异。

示例1:将宽度设置为包裹类型wrap_content或0dp

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".WeightApiUseDemoActivity"
android:orientation="horizontal"
> <Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"
/> <Button
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button 2"
/> </LinearLayout>

运行:

示例2: 将宽度设置为match_parent时

<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".WeightApiUseDemoActivity"
android:orientation="horizontal"
> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button 1"
/> <Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Button 2"
/> </LinearLayout>

运行:

小结: 第一种现象很好理解,当放置两个宽度为0dip或是wrap_content的按钮时,由于宽度并未确定,那Button1所占的宽度就是 1 / (1+2) = 1/ 3, 也就是总长度的1/3;Button2 所占的宽度是 2 / (1+2) = 2 / 3, 也就是总长度的2/3。

第二种现象就很奇怪了,其实这是因为View在绘制的时候有一个规则,如果给View添加了android:weight属性,那么这个View的最终宽度 = View本来定义的宽度 +   View在LinearLayout中剩余空间中所占比例的宽度。什么意思呢,假设线性布局的总宽度为 L ,

拿第一种现象来说

Button1的宽度 = 0dip + ( L - Button1的宽度(也就是0dip) - Button2的宽度(也是0dip) ) * 1/3 = 0dip + L * 1/3 = 1/3 L。
Button2的宽度 =  0dip + ( L - Button1的宽度(也就是0dip) - Button2的宽度(也是0dip) ) * 2/3 = 0dip + L * 2/3 = 2/3 L。

拿第二种现象来说

Button1的宽度 = L + ( L - Button1的宽度(也就是L) - Button2的宽度(也是L) ) * 1/3 = L + (-L) * 1/3 = L - 1/3 L = 2/ 3L。
Button2的宽度 = L + ( L - Button1的宽度(也就是L) - Button2的宽度(也是L) ) * 2/3 = L + (-L) * 2/3 = L - 2/3 L = 1/ 3L。

android: android 布局中的weight 属性的更多相关文章

  1. 这些Android系统样式中的颜色属性你知道吗?

    Android 系统样式中的颜色属性 推荐阅读看完后彻底搞清楚Android中的 Attr . Style .Theme 几个常用的颜色属性 先放上一张经典的图片,图片来自网络. 这张图在网上很是流传 ...

  2. 从零开始学android开发-布局中 layout_gravity、gravity、orientation、layout_weight

    线性布局中,有 4 个及其重要的参数,直接决定元素的布局和位置,这四个参数是 android:layout_gravity ( 是本元素相对于父元素的重力方向 ) android:gravity (是 ...

  3. android 在布局中动态添加控件

    第一步 final LayoutInflater inflater = LayoutInflater.from(this); 第二步:获取需要被添加控件的布局 final LinearLayout l ...

  4. android 相对布局里面的一些属性

    一.   有关于RelativeLayout布局的一些属性 1.  相对于兄弟控件的位置:android:layout_below Android:layout_toLeftof Android:la ...

  5. [转] android自定义布局中的平滑移动

    无意中搜索到这篇文章,大概扫了一眼,知道是篇好文,先转载记录下来学习! 文章主要讲的是自定义view的写法心得. 转自:http://www.apkbus.com/android-48445-1-1. ...

  6. AndroidのUI布局之layout weight

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  7. flex布局中父容器属性部分演示效果

    如图可见flex的属性分为父容器和子容器的属性共12个.关于这些属性具体代表什么意思,网上有很多教程的文章,自觉不能写得比别人更好,所以这里主要写了一些例子关于父容器属性效果的演示,希望可以帮助大家理 ...

  8. android 基本布局(RelativeLayout、TableLayout等)使用方法及各种属性

        本文介绍 Android 界面开发中最基本的四种布局LinearLayout.RelativeLayout.FrameLayout.TableLayout 的使用方法及这四种布局中常用的属性. ...

  9. android布局中显示隐藏动画

    android 在布局中提供属性,能简单的加入动画效果,例如以下: <LinearLayout ... animateLayoutChanges="true" ... /&g ...

随机推荐

  1. php与mysql交互 面向过程

    1.建立.关闭与MySQL服务器的连接 1)连接指定的mysql服务器 $mysqli_connect=@mysqli_connect($host, $user, $password,$databas ...

  2. SpringBoot2.x服务器端主动推送技术

    一.服务端推送常用技术介绍 服务端主流推送技术:websocket.SSE等 1.客户端轮询:ajax定时拉取后台数据 js   setInterval定时函数  +  ajax异步加载  定时向服务 ...

  3. Linux NFS 共享

    通过NFS网络文件系统,可以通过网络共享目录,让网络上的其他主机可以通过挂载访问共享目录的数据. Server 安装相关软件包 [root@server ~]# yum install nfs-uti ...

  4. IPC机制和生产者消费者模型

    IPC机制:(解决进程间的数据隔离问题) 进程间通信:IPC(inter-Process Comminication) 创建共享的进程列队,Queue 是多进程的安全列队,可以使用Queue 实现多进 ...

  5. logstash multiple piplines 配置方式

    当logstash有很多个input类型需要处理时.为了更方便的管理,我们需要使用一个.conf(input->filter->output)配置文件来对应一个pipeline. pipl ...

  6. Nginx基于域名的虚拟主机

    一.基于域名的虚拟主机 修改配置文件/usr/local/nginx/conf/nginx.conf 创建新的虚拟主机的根目录和默认网页index.html 重新加载nginx的配置文件 查看两个虚拟 ...

  7. 使用Arduino开发板实现与MPU6050陀螺仪传感器连接的方法

    MPU6050陀螺仪传感器具有许多强大的功能,采用单芯片封装.它是由一个MEMS加速度计.一个MEMS陀螺仪和温度传感器组成.该模块在将模拟量转换为数字量时非常准确,因为每个通道都有一个16位的模数转 ...

  8. springboot 2.x整合redis,spring aop实现接口缓存

    pox.xml: <dependency> <groupId>org.springframework.boot</groupId> <artifactId&g ...

  9. 将照片转成base64时候,使用下面的这个包更加安全一些

    import org.apache.commons.net.util.Base64; 在项目中将照片转成base64时候,使用下面的这个包更加安全一些

  10. SOLOR介绍

    https://www.cnblogs.com/ki16/p/11209508.html