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,又可以看到第三个控件。
对于这种情况还不知道问题的原因是什么。
(上面的图片好像加载有问题,估计是网络原因吧,等网络好的时候在补,想看效果的可以去我上面转的那个网址里看,或者自己试试)
在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,又可以看到第三个控件。
对于这种情况还不知道问题的原因是什么。
(上面的图片好像加载有问题,估计是网络原因吧,等网络好的时候在补,想看效果的可以去我上面转的那个网址里看,或者自己试试)
相关文章推荐
- Android 控件在布局中按比例放置
- Android 控件在布局中按比例放置 (
- Android 布局:控件放置在底部
- Android 动态添加线性布局(.java文件内) 实现控件按比例分割空间
- Android面试题:基本的UI控件和布局文件知识要点
- 关于android LinearLayout的比例布局
- android之布局控件
- Android TableLayout\TableRow内的控件占据空间的比例问题
- Android 控件布局常用属性
- Android的xml布局文件代码讲解(TextView控件)
- Android布局控件
- android:布局参数,控件属性及各种xml的作用
- Android给布局、控件加阴影效果
- Android 控件布局常用属性
- Android Fragment 中自定义键盘如何调整布局,不挡住界面控件
- (android控件)界面布局实现GridView(网格效果)
- 自定义宽高比例的布局控件
- 重拾Android 之界面控件和布局方式
- Android入门之控件布局初步
- Android入门(六):Android控件布局属性全解