您的位置:首页 > 移动开发 > Android开发

Android 控件在布局中按比例放置[转]

2014-02-19 00:12 211 查看
转自:http://netsky1990.blog.51cto.com/2220666/997452

在Android开发中常用到线性布局LinearLayout对界面进行具体的创建,其中android:layout_weight这个属性很重要,它可以按照程序员的控制,根据终端屏幕的大小,以相应的比例显示控件的大小,而不会把控件的大小写死,造成无法根据屏幕来自动调整控件本身的大小。

需要注意以下几点:
一、LinearLayout内的控件的layout_width设置为"wrap_content"
例:
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1"/>

android:layout_height="fill_parent"
android:layout_weight="2"
android:text="1"/>

android:layout_height="fill_parent"
android:layout_weight="3"
android:text="1"/>
这个时候3个TextView是按照1:2:3的比例进行显示的,但是如果TextView内的文本长度过长,则会改变效果,控件并没有按照比例显示大小,比如:

android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1111111111111111111111111111111111111111111"/>

android:layout_height="fill_parent"
android:layout_weight="2"
android:text="1"/>

android:layout_height="fill_parent"
android:layout_weight="3"
android:text="1"/>
办法是设置android:layout_width="wrap_content"为android:layout_width="0dp"。这样控件里的内容并不会影响控件的大小。
二、LinearLayout内的控件的layout_width设置为"fill_parent"
例:fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1"/>
fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:text="1"/>
这个时候整个宽度平分为3块,第一个TextView占了两块,也就是weight值越小的比例越大
当有三个控件时,问题就来了:

fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="1"/>

fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:text="2"/>

fill_parent"
android:layout_height="fill_parent"
android:layout_weight="3"
android:text="3"/>
此时第三个TextView没有显示,把上面三个TextView对应的weight分别改为2,3,4,又可以看到第三个控件。
对于这种情况还不知道问题的原因是什么。
(上面的图片好像加载有问题,估计是网络原因吧,等网络好的时候在补,想看效果的可以去我上面转的那个网址里看,或者自己试试)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: