转自: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 控件在布局中按比例放置[转]的更多相关文章

  1. 探究android控件及布局

    控件(widget) 1. TextView(该控件的一些需要注意的属性,下同) gravity="center"textSize="24sp"textColo ...

  2. android控件基本布局

    <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=&qu ...

  3. Android 控件属性介绍

    1.LinearLayout(线性布局): 可以分为水平线性:android:orientation= " horizontal " 和垂直线性:android:orientati ...

  4. Android控件属性大全(转)

    http://blog.csdn.net/pku_android/article/details/7365685 LinearLayout         线性布局        子元素任意: Tab ...

  5. Android 手机卫士--自定义组合控件构件布局结构

    由于设置中心条目中的布局都很类似,所以可以考虑使用自定义组合控件来简化实现 本文地址:http://www.cnblogs.com/wuyudong/p/5909043.html,转载请注明源地址. ...

  6. Android编程 控件与布局

    控件和布局的继承结构 常用控件 1.TextView <?xml version="1.0" encoding="utf-8"?> <Line ...

  7. android控件的属性

    android控件的属性 本节描述android空间的位置,内容等相关属性及属性的含义 第一类:属性值为true或false android:layout_centerHrizontal 水平居中 ( ...

  8. Android 控件架构及View、ViewGroup的测量

    附录:示例代码地址 控件在Android开发的过程中是必不可少的,无论是我们在使用系统控件还是自定义的控件.下面我们将讲解一下Android的控件架构,以及如何实现自定义控件. 1.Android控件 ...

  9. UIAutomator定位Android控件的方法

    UIAutomator各种控件定位的方法. 1. 背景 使用SDK自带的NotePad应用,尝试去获得在NotesList那个Activity里的Menu Options上面的那个Add note菜单 ...

随机推荐

  1. css3的transition属性的使用

    transition是将某个属性从一个属性值在指定的时间内平滑过渡到另一个属性值来实现动画效果.这个属性一般搭配:hover来使 下面看一个例子:鼠标放在div上,0.2s后将div元素的背景色用一秒 ...

  2. 用IO字节流复制文件-CopyFileByIo

    import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import ...

  3. C++ 栈 (数组实现)

    上一篇用链表实现了stack,这篇我们采用数组来存储数据,数组更容易理解,直接贴代码 第一.代码实现 #pragma once #include <iostream> using name ...

  4. 函数和object

    普通函数 在javascript中,函数是一等公民,函数在javascript是一个数据类型,而非像C#或其他描述性语言那样仅仅作为一个模块来使用. 一.函数调用形式 函数调用形式是最常见的形式,也是 ...

  5. h3c 云计算管理平台

  6. Delegate(代理)异常:该委托必须有一个目标

    转自 Delegate(代理)异常:该委托必须有一个目标 在代理调用BeginInvoke(new AsyncCallback(callBack), null);时,会抛这个异常的原因是该代理变量代理 ...

  7. Sublime Text:初学者不知道的那些事

    来源:Duchessjojo@译言 我是Sublime Text代码编辑器的忠实粉丝.我和诸多Mac程序员一样,最初使用的是Textmate代码编辑器.在Sublime Text 2发行后,我才开始转 ...

  8. SynchronizationContext

    /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : ...

  9. wamp 初始化 修改mysql密码

    1.设置phpmyadmin 在WampServer安装完成后,通过http://localhost/打开后可以看到WampServer自带的一个简单的页面,里面有phpinfo.phpmyadmin ...

  10. FP寻源报错

    ERROR [HY000] [Oracle][ODBC][Ora]ORA-01114: IO error writing block to file  (block # )  ORA-01114: I ...