一。当LinearLayout中的Button的layout_width="wrap_content"时

一切没问题,也很容易理解,btn1和btn2和btn3宽度的比就是1:2:3也就是weight的比

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/container"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="horizontal"><Buttonandroid:id="@+id/btn1"android:layout_height="fill_parent"android:layout_width="wrap_content"android:layout_weight="1"android:text="1"></Button><Button android:id="@+id/btn2"android:layout_height="fill_parent"android:layout_width="wrap_content"android:layout_weight="2"android:text="2"></Button><Button android:id="@+id/btn3"android:layout_height="fill_parent"android:layout_width="wrap_content"android:layout_weight="3"android:text="3"></Button>
</LinearLayout>

当然如果你的Button的内容太长了,长得你的weight=1满足不了“layout_width=“”wrap_content”就会出现这样:

  <Buttonandroid:id="@+id/btn1"android:layout_height="fill_parent"android:layout_width="wrap_content"android:layout_weight="1"android:text="11111111111111111111111">

解决方案:将button属性改为layout_width=”0dp“,原因是layout_height和layout_width的优先级大于layout_weight,如果layout_width="wrap_content",button就会去先满足wrap_content!~~~但你layout_width=”0dp“时不设置weigth时会报错的:Suspicious size: this will make the view invisible, should be used with layout_weight


  <Buttonandroid:id="@+id/btn1"android:layout_height="fill_parent"android:layout_width="0dp"android:layout_weight="1"android:text="11111111111111111111111"></Button>

二。当LinearLayout中的Button的layout_width="fill_parent"时


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:id="@+id/container"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="horizontal"><Buttonandroid:id="@+id/btn1"android:layout_height="fill_parent"android:layout_width="fill_parent"android:layout_weight="1"android:text="1"></Button><Button android:id="@+id/btn2"android:layout_height="fill_parent"android:layout_width="fill_parent"android:layout_weight="2"android:text="2"></Button><Button android:id="@+id/btn3"android:layout_height="fill_parent"android:layout_width="fill_parent"android:layout_weight="3"android:text="3"></Button>
</LinearLayout>


咦?btn3哪去了?来看看weight的计算公式吧,因为btn1、2、3都是layout_width=”fill_parent“,那么就少了2个parent_width啊~~(如果没有这weight,这时应是btn1布满屏幕),"之前的如果分配为fill_parent的话把,剩余的为1个parent_width (parent_width指的是屏幕宽度 )-3个parent_width=(-2个parent_width ),再按比例分配 :btn1所占空间=1个parent_width+1/6 *(-2个parent_width)=(2/3个parent_width)

btn2:1个parent_width+2/6 *(-2个parent_width)=(1/3个parent_width)"


这样一来btn1和btn2就已经占了2/3+1/3=1个parent_width了,所以就看不见btn3了

改变一下weight的比,,再看

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"android:id="@+id/container"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="horizontal"><Buttonandroid:id="@+id/btn1"android:layout_height="fill_parent"android:layout_width="fill_parent"android:layout_weight="2"android:text="1"></Button><Button android:id="@+id/btn2"android:layout_height="fill_parent"android:layout_width="fill_parent"android:layout_weight="3"android:text="2"></Button><Button android:id="@+id/btn3"android:layout_height="fill_parent"android:layout_width="fill_parent"android:layout_weight="4"android:text="3"></Button>
</LinearLayout>
恩恩,就是这样,
总之:1.layout_width=”wrap_content“时,按照weight的比例显示(内容过长时--->layout_width=”0dp“)2.layout_width=”fill_parent“时,按照上面的公式算算比例就行严重参考过的文章
点击打开链接

layout_weight如何计算比例?相关推荐

  1. 第一行代码学习笔记第三章——UI开发的点点滴滴

    知识点目录 3.1 如何编写程序界面 3.2 常用控件的使用方法 * 3.2.1 TextView * 3.2.2 Button * 3.2.3 EditText * 3.2.4 ImageView ...

  2. Android 关于Android权重的真正理解

    1.首先声明只有在Linearlayout中,layout_weight属性才有效. 在这里我们设置三个的权重比为 蓝1:黄2:红2那么它的效果是不是 蓝1:黄2:红2呢 <TextViewan ...

  3. 认识Android(常用布局,控件,四大组件,动画,自定义控件及异常消息处理机制)

    目录 一.布局 1.LinearLayout(线性布局): 2.相对布局(RelativeLayout) 3.GridLayout(网格布局) 4.FrameLayout(帧布局): 二.控件 1.T ...

  4. 考虑题4所示的日志记录_福建省厦门双十中学2016届高三上学期中考试地理【解析】...

    24.此时甲.乙.丙三地的大气垂直状况相比较 A.甲地比乙地稳定B.乙地对流最旺盛 C.乙地比丙地稳定D.丙地最稳定 25.正午前后 A.甲地气温上升最快B.乙地可能出现强对流天气 C.丙地刮起东北风 ...

  5. Android开发 入门篇(二) - 常用UI控件

    文章目录 控件 Button TextView EditText ImageView ProgressBar AlertDialog ProgressDialog 布局 LenearLayout an ...

  6. TextView控件的基本使用(各种属性的基本使用方法)

    TextView的在安卓中可以理解为一个文本视图控件,Android的视图控件的基类是View类,可以理解的TextView是View的子类.我们通常在.XML布局文件中会为文本视图控件指定各种属性来 ...

  7. Unity 2018导入SketchUp模型

    Unity2018版本直接支持读取SketchUp的模型,以下取自官网 SketchUp设置 SketchUp是常用于建模的软件.Unity2018直接读取SketchUp文件并支持以下SketchU ...

  8. Android入门基础2

    回顾: 使用代码逻辑与界面交互 控件的获取 点击事件的处理 kotlin语法 val变量设置 when语法 重点: 点击事件的设置 控件对象的获取 线性布局------LinearLayout 内部控 ...

  9. Android知识补充(Android学习笔记)

    Android知识补充 ●国际化 所谓的国际化,就是指软件在开发时就应该具备支持多种语言和地区的功能,也就是说开发的软件能同时应对不同国家和地区的用户访问,并针对不同国家和地区的用户,提供相应的.符合 ...

最新文章

  1. JavaMVC 模式
  2. zoj 1698 Easier Done Than Said?
  3. 【转】NSDictionary以及NSMutableDictionary的用法
  4. java二级考试历年真题6_计算机等级考试二级JAVA练习题及答案6
  5. Http协议中的方法
  6. word如何设置长宽高_word怎么设计表格高和宽
  7. 项目中的一个技术方案替换历程(surfaceview+fragment 变成悬浮窗window)
  8. linux module 目录,/sys/module/ 模块信息目录与/proc/modules文件
  9. Python中的枚举(enumerate)
  10. 本地使用TensorBoard
  11. 2019 live tex 发行版_TeX Live 2019安装指南
  12. python 模拟浏览器selenium_浅谈python爬虫使用Selenium模拟浏览器行为
  13. Android的ALSA声卡
  14. [CTF]RAR加密原理
  15. 长白山沟谷地带珍贵药材选址设计实习
  16. 输出某年某月某日的c语言程序,输入某年某月某日,输出这是今年的第几天,C语言问题...
  17. 银联支付(chinapay)java接入避坑指南
  18. android gpu 视频编码,Android Mp4视频录制(OpenGL实现篇,附DEMO)
  19. 深入探索透视投影变换(续)
  20. Win10问题篇:解决电脑连不上网的问题。

热门文章

  1. 计算机专业博士推荐信模板,计算机推荐信模板
  2. mac系统下,怎么装win7系统?
  3. python实验指导书燕山大学答案_Python实验指导书课件.doc
  4. LBS在智能交通系统中的应用
  5. Symbian OS 开发初级手册 9(转)
  6. 新系统开发流程与迭代
  7. red flag 5.0 安装
  8. 《电气图用图形符号》
  9. html 安装字体无效果,如何在HTML网站上安装自定义字体
  10. Android直播的集成