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. PHP学习(4)——数据类型

    PHP 支持 8 种原始数据类型. 四种标量类型:(标量类型即为基本类型) boolean(布尔型) integer(整型) float(浮点型,也称作 double) (由于历史原因,float也叫 ...

  2. Writing Sentences [1]

    1) try 'there will be' instead of 'then' In homogeneity MRF, if and , there will be even if . 2) in ...

  3. Android图表库MPAndroidChart(七)—饼状图可以再简单一点

    Android图表库MPAndroidChart(七)-饼状图可以再简单一点 接上文,今天实现的是用的很多的,作用在统计上的饼状图,我们看下今天的效果 这个效果,我们实现,和之前一样的套路,我先来说下 ...

  4. Android Multimedia框架总结(十五)Camera框架之Camera2补充

    转载请把头部出处链接和尾部二维码一起转载,本文出自逆流的鱼yuiop:http://blog.csdn.net/hejjunlin/article/details/52751055 前言:监于5.0之 ...

  5. 从源码安装git

    蛋疼的阿里云,git版本居然才1.9.只能手动安装了. 预装 apt-get update apt-get --yes install libcurl4-gnutls-dev libexpat1-de ...

  6. list标准函数的模拟

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

  7. android 网络获取json并且显示(2)

    1.将要的取得的json数据格式如下: 我们封装之前的类用google提供的JSONArray和JSONObject类对json字符串进行解析. 对于姚明显示每一条数据,我们封装了一个类如下: pub ...

  8. Erlang标准数据结构的选择

    Erlang标准数据结构的选择(金庆的专栏)gen_server with a dict vs mnesia table vs etshttp://stackoverflow.com/question ...

  9. java.io.FileNotFoundException: ..\lib\commons-el.jar

    安装openfire成功后,启动遇到java.io.FileNotFoundException: ..\lib\commons-el.jar错误,并不是缺少了jar包,只需以管理员身份运行即可解决.

  10. 在代码中写view 的长宽高等

    获得资源的id的另一种方法 int layoutRes = getResources().getIdentifier("pager_view" + i, "layout& ...