zoukankan      html  css  js  c++  java
  • Android 控件在布局中按比例放置[转]

    转自: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,又可以看到第三个控件。
        对于这种情况还不知道问题的原因是什么。
        (上面的图片好像加载有问题,估计是网络原因吧,等网络好的时候在补,想看效果的可以去我上面转的那个网址里看,或者自己试试)
  • 相关阅读:
    day30 python类的继承,抽象类等
    20170702-变量说明,静态方法,类方法区别,断点调试,fork,yield协程,进程,动态添加属性等。。
    day29 面向对象入门
    day28 import,from * import *,__name__
    day27 模块:正则re, configparser, subprocess
    day26 re正则表达式
    MD5的学习与练习
    HBase
    Struts13---Ognl
    Struts12---文件的下载
  • 原文地址:https://www.cnblogs.com/sunshine-anycall/p/3555125.html
Copyright © 2011-2022 走看看