layout_weight(TabHost为什么要在FrameLayout里面加上这个属性 android:layout_weight=“1“它的选项卡才在底部否则在上面)

本文目录
- TabHost为什么要在FrameLayout里面加上这个属性 android:layout_weight=“1“它的选项卡才在底部否则在上面
- 为什么android layout_weight属性可以把控件按一定的比例进行布局
- 不在xml中配置android:layout_weight,而是写在java代码中如何实现权重的分配
- html 中是否有类似安卓的match_parent, layout_weight等属性呢
- LinearLayout布局下android:layout_weight用法
TabHost为什么要在FrameLayout里面加上这个属性 android:layout_weight=“1“它的选项卡才在底部否则在上面
layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。
所有的视图都有一个layout_weight值,默认为零,意思是需要显示
多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视
图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight
值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布
局的layout_weight值中所占的比率而定。
举个例子:比如说我们在 水平方向上有一个文本标签和两个文本元素。
该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。
如果两个文本元素每一个的layout_weight值都设置为1,则两者平分
在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个
文本元素其中第一个的layout_weight值设置为1,而第二个的设置为2,
则剩余空间的三分之二分给第一个,三分之一分给第二个(数值越小,重要度越高)。
为什么android layout_weight属性可以把控件按一定的比例进行布局
下Layout_weight属性的作用:它是用来分配属于空间的一个属性,你可以设置他的权重。
SDK中的解释:
Indicates how much of the extra space in the LinearLayout will be allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
重点有两个
layout_weight表示LinearLayout中额外空间的划分(可能扩大应用layout_weight前的大小也可能缩小)。
按比例(layout_weight大小的比例)。
以下说的都以 android:orientation=“horizontal“ 为例
看了一下源码,虽说不太懂,但了解了下大概意思,按照自己的理解总结一下,直接写一下简化的代码吧(下面的代码是LinearLayout源文件中一部分的精简,变量名称含义可能不准确,为叙述方便暂作此解释):
不在xml中配置android:layout_weight,而是写在java代码中如何实现权重的分配
editText.setLayoutParams(new LinearLayout.LayoutParams(width, height, weight));
只有LinearLayout才有layout_weight属性。
html 中是否有类似安卓的match_parent, layout_weight等属性呢
html里面没有这种属性,不过可以直接给宽高设置百分比,效果一样。
match_parent === widht:100%;
layout_weight === widht:50;widht:50;
LinearLayout布局下android:layout_weight用法
layout_weight意思是布局比重的意思,在线性布局中常用layout_weight,分割布局。
通常线性布局中宽高布局常用android:layout_width=match_parent|wrap_content,android_height=match_parent|wrap_content来进行布局,如果要用比重布局,通常android:layout_width属性就会不起作用,设置为“0“;根据想要布局的比例,设定android:layout_weight的值,值越大,占的布局就越大。
考虑到Android多版本的兼容问题,通常使用match_parent而不使用fill_parent.

本文相关文章:
postscript教程(postscript用法!急求!)
2026年9月6日 06:10
microsoft sql(Microsoft SQL Server是什么软件)
2026年9月4日 13:10
Host是什么意思?如何使用route命令查看 Linux内核路由表
2026年8月26日 13:20
word中如何设置页码(microsoft word文档怎么设置页码)
2026年8月22日 09:50
更多文章:
易语言网页api接口怎么调用(易语言,怎么读取网页json的api)
2026年10月11日 08:00
majority of(the majority of 和 a majority of的区别以及用法例句)
2026年10月11日 07:40
another time(another time和other time的区别)
2026年10月11日 05:00




