1、概述

在线性布局有时候为了控制一下屏幕的适配,可以使用layout_weight这个属性来设置权重,要注意一点,这个属性只有在Linearlayout中才有效,这个属性往往会随着android:layout_width设置而变化,有时候可能出现一些意想不到的效果,下面来看看

2、实例效果

布局文件如下,水平放了2个文本,设置不同权重1:2

2.1、layout_width=“0dp”

<span style="font-size:18px;"><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=".MainActivity">

    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="aaa"
        android:background="@android:color/holo_blue_bright"
        android:textSize="20sp" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="2"
        android:background="@android:color/holo_green_dark"
        android:text="bbb"
        android:textSize="20sp" />

</LinearLayout></span>

正常1:2权重显示布局

2.2、layout_width=“match_parent”

width改成match_parent

惊奇的发现,说好的1:2咋变成2:1了呢,

咋回事呢?下面会详细解释

2.3、layout_width=“wrap_content”

此时又恢复正常

3、解开谜底

【原则】Linearlayout中的layout_weight属性首先按照控件声明的尺寸进行分配,然后将剩下的尺寸按照weight分配

这里看出layout_width=“0dp”和layout_width=“wrap_content”是一样的,他们是把宽度交给实际控件的宽度起始都为0;

而layout_width=“match_parent”是将控件在一开始就设置成了父控件Linearlayout的大小假设为S,,俩个控件都是match_parent,则对应每个TextView控件一开始大小就为S

那个剩下的尺寸为:原屏幕尺寸减去控件占有尺寸,即S-(S+S)= -S ;此时第一个TextVIew占1/3权重,则实际宽度为S+(-S)*1/3 = S* 2/3,同理可知第二个TextView占S*1/3。

这下谜底解开了吧。Google在官方推荐,当使用layout_weight属性时,将width设为0dip即可,效果跟设成wrap_content是一样的。

4、其他

单一控件实现权重布局

如果我们只有一个TextView要想实现这一个控件占1/2权重怎么办?此时没有其他控件跟我们均分,此时可以在Linearlayout中将android:weightSum="2",然后在texiview中权重设置为1即可

布局文件为

<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"
    android:weightSum="2"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@android:color/holo_blue_bright"
        android:text="aaa"
        android:textSize="20sp" />

</LinearLayout>

效果图为

神奇的layout_weight属性的更多相关文章

  1. android:layout_weight属性详解 (转)

    在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示.android并没用提 ...

  2. android:layout_weight属性详解(转)

    在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示.android并没用提 ...

  3. 50一个Android开发技巧(01 利用好layout_weight属性)

    问题:如何将一个Button放置在布局的中间,并设置其宽度parent的50%? 分析:问题想要达到的效果应该是这样: (原文地址:http://blog.csdn.net/vector_yi/art ...

  4. 日积月累:weightSum和layout_weight属性合用

    解说一:weightSum和layout_weight属性合用 android:weightSum属性:定义weight总和的最大值. 假设为指定该值,全部子视图的layout_weight属性的累加 ...

  5. android:layout_weight属性的使用方法总结

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6282826.html android:layout_weight属性可以和其他属性配合使用,产生多 ...

  6. Android知识点剖析系列:深入了解layout_weight属性

    摘录自:http://www.cnblogs.com/net168/p/4227144.html 前言 Android中layout_weight这个属性对于经常捣鼓UI的我们来说,肯定不会陌生.但是 ...

  7. android:layout_weight属性详解

    weight:重量.权重. 当我们给一个view设置了android:layout_weight属性,意味着赋予它话语权,常规思维就是谁的weight大,谁说了算(空间占比大). 下面我们来看下具体的 ...

  8. Android 布局学习之——LinearLayout的layout_weight属性

    一直对layout_weight属性感到比较困惑,今天学习一下,来深入了解layout_weight属性和它的用法.     定义     首先,看看Android官方文档是怎么说的,毕竟人家才是权威 ...

  9. Android Layout_weight 属性

    Android 对Layout_weight属性完全解析以及使用ListView来实现表格http://blog.csdn.net/xiaanming/article/details/13630837 ...

随机推荐

  1. JAVA处理Blob大对象

    Blob对象是SQL Blob的Java语言映射.SQL Blob是一个内置类型,它可以将一个二进制大对象保存在数据库中.接口ResultSet.CallableStatement和PreparedS ...

  2. Android中典型的ROOT原理(5)

    ROOT的作用 Customization 用户的个人定制,如删除一些预安装,定制开机动画等. 特权操作 所有需要特权操作的基本都是要通过ROOT,这也是ROOT的初衷. ROOT的第一步:寻找漏洞并 ...

  3. RDO Stack Exception: UnboundLocalError: local variable 'logFile' referenced before assignment

    Issue: When you install RDO stack on CentOS, you may encounter following error. Error: [root@localho ...

  4. How to kill a particular user terminal on Linux

    Intro. Sometimes, the application we launched from command promp failed to exit. What we require is ...

  5. list标准函数的模拟

    ;反序 ( ) -> ( ) (define (rvs x) (let recur ((x x)(res '())) (if (null? x) res (recur (cdr x) (cons ...

  6. 开源框架Volley的使用《二》[NetWorkImageView&&LruCache&ImageLoader]

    转载本专栏每一篇博客请注明转载出处地址,尊重原创.此博客转载链接地址:小杨的博客    http://blog.csdn.net/qq_32059827/article/details/5278849 ...

  7. J2EE进阶(十四)超详细的Java后台开发面试题之Spring IOC与AOP

    J2EE进阶(十四)超详细的Java后台开发面试题之Spring IOC与AOP 前言   搜狐畅游笔试题中有一道问答题涉及到回答谈谈对Spring IOC与AOP的理解.特将相关内容进行整理.    ...

  8. 如何在苹果手机上安装自制的AD证书

    写这篇博文的契机是有人已经实现了CRM在用自制证书部署IFD后,在手机安装上自制证书后即可登录官方移动端APP,因为之前很多人都尝试过只要是自制证书部署的IFD就无法使用官网手机APP,而本人实验下来 ...

  9. android连接打印机

    android连接  网络打印,主要使用socket连接设备,发送指令给设备. 首先要有设备的IP,端口号一般默认的是9100 //打印设备网络IP etIp.setText("192.16 ...

  10. Android项目开发填坑记-9patchPng报错

    如果阅读体验不佳,请使用–> Github版 背景 之前写了一篇文章Android必知必会–NinePatch图片制作详细介绍了Android 9Patch图片的制作和一些Demo展示,这次说明 ...