android layout_width 属性,android:layout_weight属性详解

在android开发中LinearLayout很常用,LinearLayout的内控件的android:layout_weight在某些场景显得非常重要,比如我们需要按比例显示。android并没用提供table这样的控件,虽然有TableLayout,但是它并非是我们想象中的像html里面的table那么好用,我们常用ListView实现table的效果,但是列对齐确比较麻烦,现在用LinearLayout及属性android:layout_weight能很好地解决。下面我们共同体验下layout_weight这个属性。一、LinearLayout内的控件的layout_width设置为"wrap_content",请看一下xml配置:

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

>

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="#aa0000"

android:gravity="center"

android:text="1"/>

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#00aa00"

android:gravity="center"

android:text="1"/>

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="3"

android:background="#0000aa"

android:gravity="center"

android:text="1"/>

** 效果如下:**

52bafc8b4b00

android:layout_weight属性详解

可以看到这三个TextView是按照1:2:3的比例进行显示的,这样看来似乎可以实现按照比例显示了,但是有个问题,如果TextView内的文本长度一同那么较长文本的TextView会宽度会有所增加,见下面配置及效果:

配置:

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="#aa0000"

android:gravity="center"

android:text="1111111111111111111111111111111111111111111"/>

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#00aa00"

android:gravity="center"

android:text="2"/>

android:layout_width="wrap_content"

android:layout_height="fill_parent"

android:layout_weight="3"

android:background="#0000aa"

android:gravity="center"

android:text="3"/>

效果:

52bafc8b4b00

android:layout_weight属性详解

这样看来我们所需要的按比例又无法实现了,经过满天地google终于找到了解决方案,就是设置layout_width设置为"wrap_content"。配置及效果见下:

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="#aa0000"

android:gravity="center"

android:text="1111111111111111111111111111111111111111111"/>

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#00aa00"

android:gravity="center"

android:text="2"/>

android:layout_width="0dp"

android:layout_height="fill_parent"

android:layout_weight="3"

android:background="#0000aa"

android:gravity="center"

android:text="3"/>

效果:

52bafc8b4b00

android:layout_weight属性详解

这样终于达到我们的按比例显示的效果了,感觉很是奇怪,android开发框架的大佬们有时候设计确实有点匪夷所思。

二、LinearLayout内的控件的layout_width设置为"fill_parent",请看一下xml配置:

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="#aa0000"

android:gravity="center"

android:text="1"/>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#00aa00"

android:gravity="center"

android:text="2"/>

效果如下:

52bafc8b4b00

android:layout_weight属性详解

奇怪吧,整个宽度平分3块,第一个TextView占了两块,这样看来weight值越小的优先级越大。只有两个TextView似乎看出些道理,那么让我们看看三个是什么效果:

android:orientation="horizontal"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1">

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="1"

android:background="#aa0000"

android:gravity="center"

android:text="1"/>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="2"

android:background="#00aa00"

android:gravity="center"

android:text="2"/>

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:layout_weight="3"

android:background="#0000aa"

android:gravity="center"

android:text="3"/>

效果:

52bafc8b4b00

android:layout_weight属性详解

什么意思?第三个TextView丢掉了,很是奇怪,让我们再试一个,把weight分别改为2,3,4的看看效果:

52bafc8b4b00

android:layout_weight属性详解

这个效果让人很困惑,我一直想寻求出一个确切的比例公式,但是至今未找到。有哪位大神能搞定的话忘不吝赐教。

虽然这个android:layout_weight属性很怪异,但幸运的是我们达到了目标:

按比例显示LinearLayout内各个子控件,需设置android:layout_width="0dp",如果为竖直方向的设置android:layout_height="0dp"。在这种情况下某子个控件占用LinearLayout的比例为:本控件weight值 / LinearLayout内所有控件的weight值的和。

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543004.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

angular的$http发送post,get请求无法传送参数的问题

2019独角兽企业重金招聘Python工程师标准>>> 用$http进行异步请求的时候发现了一个奇怪的事情,用$http.post(url,data)的方法进行请求,后台死活接收不到data的参数,真是百思不得姐啊..... 折腾了老半天才在stackoverflow上找到答案…

python变量和常量_Python数学模块常量和示例

python变量和常量Python数学模块常量 (Python math module constants) In the math module, there are some of the defined constants that can be used for various mathematical operations, these are the mathematical constants and returns their values equivalent to …

怎样解决Word文档图标无法正常显示的问题?

此类问题是由于 Word 程序相关组件损坏导致,可以通过下面的方案来解决:步骤/方法按键盘上的 Windows 徽标健 R 键,输入 regedit,按回车键。(若弹出用户账户控制窗口,请允许以继续)对于 Word 200…

android 对话框的父view是谁,android – 在对话框中获取相对于其父级的视图位置

我想要做的是,从按钮边缘到屏幕上的一点画一条线……我正在使用一个对话框片段…我尝试的所有函数总是返回0点…我试过以下:Overrideprotected Dialog createDialog(Bundle savedInstanceState, FragmentActivity activity){Dialog d builder.create();View v Lay…

np.radians_带有Python示例的math.radians()方法

np.radiansPython math.radians()方法 (Python math.radians() method) math.radians() method is a library method of math module, it is used to convert angle value from degree to radians, it accepts a number and returns the angle value in radians. math.radians(…

怎么用git将本地代码上传到远程服务器_git之如何把本地文件上传到远程仓库的指定位置...

2018.11.26添加内容:对于自己的仓库,我们建议将远程仓库通过clone命令把整个仓库克隆到本地的某一路径下。这样的话我们从本地向远程仓库提交代码时,就可以直接把需要提交的文件拖到我们之前克隆下来的路径下,接下来在这整个仓库下…

MathType与Origin是怎么兼容的

MathType作为一款常用的公式编辑器,可以与很多的软件兼容使用。Origin虽然是一款专业绘图与数据分析软件,但是在使用过程中也是可以用到MathType。它可以帮助Origin给图表加上标签,或者在表格中增加公式标签。但是一些用户朋友对这个不能不是…

c语言 函数的参数传递示例_llround()函数以及C ++中的示例

c语言 函数的参数传递示例C llround()函数 (C llround() function) llround() function is a library function of cmath header, it is used to round the given value and casts to a long long integer, it accepts a number and returns the integer (long long int) valu…

android requestmtu,android - 如何设置/获取/请求从Android到iOS或反之亦然BLE的MTU? - 堆栈内存溢出...

我们正在将MTU请求从Android发送到iOSAndroid-从此函数onServicesDiscovered回调请求MTU但是我不知道如何确定对等设备支持是否请求了MTU,以及如何实际协商的MTU。 仅在API级别22(Android L 5.1)中添加了必需的函数:BluetoothGattCallback.onMtuChanged(…

AutoBookmark Adobe Acrobat快速自动批量添加书签/目录

前言 解决问题:Adobe Acrobat快速自动批量添加书签/目录, 彻底告别手动添加书签的烦恼 AutoBookmark 前言1 功能简介2 实现步骤2.1 下载插件2.2 将插件复制到Acrobat文件夹下2.3 自动生成书签 1 功能简介 我们在查看PDF版本的论文或者其他文件的时候, 虽然相比较于…

Python调用微博API获取微博内容

一:获取app-key 和 app-secret 使用自己的微博账号登录微博开放平台(http://open.weibo.com/),在微博开放中心下“创建应用”创建一个应用,应用信息那些随便填,填写完毕后,不需要提交审核,需要的只是那个ap…

python独立log示例_带有Python示例的math.log1p()方法

python独立log示例Python math.log1p()方法 (Python math.log1p() method) math.log1p() method is a library method of math module, it is used to get the natural logarithm of 1x (base e), it accepts a number and returns the natural logarithm of 1number on base e…

15947884 oracle_Oracle Patch Bundle Update

一、相关知识介绍以前只知道有CPU(Critical Patch Update)和PSU(Patch Set Update),不知道还有个Bundle Patch,由于出现了TNS-12531的BUG问题,需要在windows上打至少为Patch bundle 22补丁。通过学习查找:Oracle里的补丁具体分为如下这样6种类型&#xf…

鸿蒙系统hdc,HDC2020有看头:要揭开鸿蒙系统和EMUI11神秘面纱?

IFA2020算是HDC2020的预热吧,一个是9月2日在德国柏林举办的消费电子展,一个是在松山湖举办的华为开发者大会,二者的目的都一样,但也有一丝不同,IFA是为了让老外了解HMS、了解华为的智慧生态,而HDC2020就是要…

UVA 12501 Bulky process of bulk reduction ——(线段树成段更新)

和普通的线段树不同的是,查询x~y的话,给出的答案是第一个值的一倍加上第二个值的两倍一直到第n个值的n倍。 思路的话,就是关于query和pushup的方法。用一个新的变量sum记录一下这个区间里面按照答案给出的方式的值,比如说&#xf…

gdb ldexp_带有Python示例的math.ldexp()方法

gdb ldexpPython math.ldexp()方法 (Python math.ldexp() method) math.ldexp() method is a library method of math module, it is used to calculate expression x*(2**i), where x is a mantissa and i is an exponent. It accepts two numbers (x is either float or inte…

windows安装包删了会有影响吗_win7系统删除系统更新安装包的详细教程

win7系统使用久了,好多网友反馈说win7系统删除系统更新安装包的问题,非常不方便。有什么办法可以永久解决win7系统删除系统更新安装包的问题,面对win7系统删除系统更新安装包的图文步骤非常简单,只需要1.其实在win7旗舰版系统中&a…

解压android img文件怎么打开,解压压缩android img文件

boot.imgboot和recovery映像并不是一个完整的文件系统,它们是一种android自定义的文件格式,该格式包括了2K的文件头,后面紧跟着是用gzip压缩过的内核,再后面是一个ramdisk内存盘,ramdisk映像是一个最基础的小型文件系统…

Java String 学习笔记 (一)

2019独角兽企业重金招聘Python工程师标准>>> ###String 简介 String 并非java的8大基本数据类型之一。 java中基本数据类型存储在栈内存中。而String不是,新new的String 对象存储在堆内存中。而字符串存储在常量池中。String对象的引用存储中栈内存中。 …

tau nb_math.tau常数,带Python示例

tau nbPython math.tau常数 (Python math.tau constant) math.tau constant is a predefined constant, which is defined in math module, it returns the value of mathematical constant "τ" (Tau), the value is 6.283185307179586 math.tau常数是在数学模块中定…