博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 控件在布局中按比例放置[转]
阅读量:7117 次
发布时间:2019-06-28

本文共 1860 字,大约阅读时间需要 6 分钟。

转自: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,又可以看到第三个控件。
    对于这种情况还不知道问题的原因是什么。
    (上面的图片好像加载有问题,估计是网络原因吧,等网络好的时候在补,想看效果的可以去我上面转的那个网址里看,或者自己试试)
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!
本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/p/3555125.html
,如需转载请自行联系原作者
你可能感兴趣的文章
ubuntu(14.04) 网路管理
查看>>
vue按需引入element Transfer 穿梭框
查看>>
Facebook 2018 年度开源回顾:新增开源项目 153 个
查看>>
JDBC的数据类型
查看>>
「镁客·请讲」Ayla米歇尔·马埃索:在物联网,我们要做一个“中心环节”
查看>>
PiFlow v0.5 发布:大数据流水线系统
查看>>
iOS__上传应用到AppStore出现Authenticating with the iTunes store
查看>>
mac下设置eclipse自动提示
查看>>
IntelliJ IDEA中日志分类显示设置
查看>>
数据结构思维 第十六章 布尔搜索
查看>>
独家|从满天飞舞到逐步落地,自动驾驶好消息只会越来越多
查看>>
如何找到后台运行的隐藏程序
查看>>
多维防护:虚拟化安全挑战的破解之道
查看>>
Maven在Eclipse中的实用小技巧
查看>>
JdbcTemplate使用小结
查看>>
2014 网选 5011 Game(Nim游戏,数学题)
查看>>
微软官方windows phone开发视频教程第一天视频(附下载地址)
查看>>
螺旋阵列
查看>>
Gut基础入门(十)Git远程分支
查看>>
VC编写的程序不能在其他机器上运行的解决方案(续)
查看>>