首先看一下LinearLayout布局中Layout_weight属性的作用:它是用来分配属于空间的一个属性,你可以设置他的权重。很多人不知道剩余空间是个什么概念,下面我先来说说剩余空间。
看下面代码:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="left" android:text="one"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="center" android:layout_weight="1.0" android:text="two"/> <EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:gravity="right" android:text="three"/> </LinearLayout>
运行结果是:
看上面代码:只有Button2使用了Layout_weight属性,并赋值为了1,而Button1和Button3没有设置Layout_weight这个属性,根据API,可知,他们默认是0
下面我就来讲,Layout_weight这个属性的真正的意思:Android系统先按照你设置的3个Button高度Layout_height值wrap_content,给你分配好他们3个的高度,
然后会把剩下来的屏幕空间全部赋给Button2,因为只有他的权重值是1,这也是为什么Button2占了那么大的一块空间。
有了以上的理解我们就可以对网上关于Layout_weight这个属性更让人费解的效果有一个清晰的认识了。
我们来看这段代码:
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal" > <TextView android:background="#ff0000" android:layout_width="**" android:layout_height="wrap_content" android:text="1" android:textColor="@android:color/white" android:layout_weight="1"/> <TextView android:background="#cccccc" android:layout_width="**" android:layout_height="wrap_content" android:text="2" android:textColor="@android:color/black" android:layout_weight="2" /> <TextView android:background="#ddaacc" android:layout_width="**" android:layout_height="wrap_content" android:text="3" android:textColor="@android:color/black" android:layout_weight="3" /> </LinearLayout>
三个文本框的都是 layout_width=“wrap_content ”时,会得到以下效果
按照上面的理解,系统先给3个TextView分配他们的宽度值wrap_content(宽度足以包含他们的内容1,2,3即可),然后会把剩下来的屏幕空间按照1:2:3的比列分配给3个textview,所以就出现了上面的图像。
而当layout_width=“fill_parent”时,如果分别给三个TextView设置他们的Layout_weight为1、2、2的话,就会出现下面的效果:
你会发现1的权重小,反而分的多了,这是为什么呢???网上很多人说是当layout_width=“fill_parent”时,weighth值越小权重越大,优先级越高,就好像在背口诀
一样,其实他们并没有真正理解这个问题,真正的原因是Layout_width="fill_parent"的原因造成的。依照上面理解我们来分析:
系统先给3个textview分配他们所要的宽度fill_parent,也就是说每一都是填满他的父控件,这里就死屏幕的宽度
那么这时候的剩余空间=1个parent_width-3个parent_width=-2个parent_width (parent_width指的是屏幕宽度 )
那么第一个TextView的实际所占宽度应该=fill_parent的宽度,即parent_width + 他所占剩余空间的权重比列1/5 * 剩余空间大小(-2 parent_width)=3/5parent_width
同理第二个TextView的实际所占宽度=parent_width + 2/5*(-2parent_width)=1/5parent_width;
第三个TextView的实际所占宽度=parent_width + 2/5*(-2parent_width)=1/5parent_width;所以就是3:1:1的比列显示了。
这样你也就会明白为什么当你把三个Layout_weight设置为1、2、3的话,会出现下面的效果了:
第三个直接不显示了,为什么呢?一起来按上面方法算一下吧:
系统先给3个textview分配他们所要的宽度fill_parent,也就是说每一都是填满他的父控件,这里就死屏幕的宽度
那么这时候的剩余空间=1个parent_width-3个parent_width=-2个parent_width (parent_width指的是屏幕宽度 )
那么第一个TextView的实际所占宽度应该=fill_parent的宽度,即parent_width + 他所占剩余空间的权重比列1/6 * 剩余空间大小(-2 parent_width)=2/3parent_width
同理第二个TextView的实际所占宽度=parent_width + 2/6*(-2parent_width)=1/3parent_width;
第三个TextView的实际所占宽度=parent_width + 3/6*(-2parent_width)=0parent_width;所以就是2:1:0的比列显示了。第三个就直接没有空间了。
相关推荐
如果LinearLayout是最外面的一层,它是不会弹出layout_weight属性的, 换句话说最外层不能用layout_weight xml布局如下 <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android android:...
几乎没有用到,我们完全可以用LinearLayout和RelativeLayout来代替TableLayout的使用,自己开发中主要使用LinearLayout,RelativeLayout这两种布局,不过刚开始我还是偏爱于RelativeLayout,因为在RelativeLayout...
android:layout_weight="1" android:layout_height="wrap_content" android:text="行1列2" /> <TextView android:layout_width="wrap_content" android:layout_weight="1" android:layout_height="wrap_content...
废话不多少了,先上布局,布局文件非常简单,一个EditText和一个Button被填充在LinearLayout中其余部分全部留给WebView ``` xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_...
LinearLayout是线性布局控件:要么横向排布,要么竖向排布 常用属性: android:gravity————设置的是控件自身上面的内容位置 android:layout_gravity—–设置控件本身相对于父控件的显示位置 android:layout_...
<LinearLayout nandroid:layout_width=“0dp” android:layout_weight u201c1” android:layout_height=“match_parent” android:background> 运行效果如下 因为灰色部分和黑色部分权重相同故两部分的宽度各占总...
为了创建比例大小的子View,可以将LinearLayout的宽度和...对于LinearLayout的几个子View,将它们的宽度都定义为0,android:layout_width=”0dip”,然后使用layout_weight 为每个View指定宽度比例,本例为每个TextVie
import android.content.ContentResolver; import android.content.Intent; import android.graphics.Bitmap;... android:layout_weight="1" /> </LinearLayout> </android.support.constraint.ConstraintLayout>
-- linearlayout中的布局是每一项后面隐藏的布局 --> <LinearLayout android:id="@+id/back" android:layout_width="match_parent" android:layout_height="80dp" android:background="#eee" android:tag="back" >...
android:layout_weight="1"> android:layout_width="match_parent" android:layout_height="0.5dp" android:background="#737373" /> android:layout_width="match_parent" android:layout_height=...
这个是三联生活周刊应用布局效果的源码,android自定义viewgroup实现等分格子布局,实现这样的效果:一般的思路就是,直接写布局文件,用LinearLayout 嵌套多层子LinearLayout,然后根据权重layout_weight可以达到上面...
android:layout_weight="1" android:layout_width="0dp" android:layout_height="match_parent"/> android:onClick="scrollTo3" android:background="@android:color/holo_green_light" android:...
android:layout_weight="1" android:layout_height="wrap_content" android:layout_marginLeft="15dp" android:layout_marginTop="5dp" android:layout_marginBottom="5dp" android:layout_marginRight="10dp...
它提供了控件水平垂直排列的模型,同时可以通过设置子控件的 weight 布局参数控制各个控件在布局中的相对大小。LinearLayout 有两种排列方式:水平排列(horizontal)和垂直排列(vertical)。 在 LinearLayout 中...
xml布局中创建CircleIndicator是配合ViewPager使用的,一般如下布局: android:layout_width="match_parent" android:layout_height="match_parent"> android:id="@ id/viewpager_default" ...
一个Android计算器实例,布局文件中用到了Linearlayout、TableLayout、android:layout_weight的嵌套
用Android Studio 2.3.3做的线性布局例子,主要用到的标签有:<LinearLayout></LinearLayout>;主要用到的设置有:android:orientation="horizontal"、android:layout_weight="1"。
EmotionInputDetector 一个用于开发情感输入布局的库,例如微信,电报等 下载 ... 将高度可变的视图/布局的layout_height设置为0,并将layout_weight设置为1 2.在活动布局的底部添加自定义的情绪输
android中对weight的学习可以说是必须的,如果UI布局仅仅使用dp与sp等等,会让布局显得极度不灵活,毕竟各个手机屏幕大小不同,更别说是还有ipad之类的了,所以也是同做本人近期做的一个小UI来分享一下weight的使用...
ChildLayout描述为了解决当我们使用 LinearLayout 布局的时候使用了 weight 属性后,再嵌套LinearLayout,又一次在子 LinearLayout 中使用 weight 属性,会导致实际的 View 无法被成功绘制的解决方案,具体实际的...