都知道weight是权重的意思. 在布局中起到非常重要的作用. 但是这玩意不能嵌套使用, 而且只能使用在LinearLayout中.

  下面说说它的几种用法(以下例子全为横排 注意android:layout_width值和android:layout_weight值的变化)  

  第一种, 最普遍的-----均分, weight的值越大, 占的空间越大.注意android:layout_width的值都为0dp

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000" /> <Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/> <FrameLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#888" /> </LinearLayout>

   第二种, 占满剩余空间, 不管是否处在最后一个

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"> <FrameLayout
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="#000" /> <Button
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"/> <FrameLayout
android:layout_width="40dp"
android:layout_height="match_parent"
android:background="#888" /> </LinearLayout>

  第三种, 值越大占的空间越小, 值为同布局下其它控件的weight之和时,即消失.注意android:layout_width的值都为match_parent为0时占满. 有什么用途? 呵呵, 存在即是合理的.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"> <FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000" /> <Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.5"/> <FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#000" /> </LinearLayout>

  最后一种(我会用的), 结合android:weightSum使用, 那这儿就用一个问题引入, 如何将控件放在屏幕的中央, 宽度为屏幕的一半?  嗯.....想啊.....想啊...RelativeLayout? 哦, 没法控制? 哦,只能代码了... 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:weightSum="2"> <Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/> </LinearLayout>

ok, so easy!

android:layout_weight总有你不知道的用法.的更多相关文章

  1. 【Android学习】android:layout_weight的用法实例

    对于android:layout_weight的用法,用下面的例子来说明: <LinearLayout xmlns:android="http://schemas.android.co ...

  2. Android layout_weight的用法

    android:layout_weight是指LinearLayout先给里面的控件分配完大小之后剩余空间的权重. 下面通过举例说明: <LinearLayout xmlns:android=& ...

  3. Android Drawable 那些不为人知的高效用法

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/43752383,本文出自:[张鸿洋的博客] 1.概述 Drawable在我们平时的 ...

  4. android:layout_weight的真实含义(转)

    首先声明只有在Linearlayout中,该属性才有效.之所以Android:layout_weight会引起争议,是因为在设置该属性的同时,设置android:layout_width为wrap_c ...

  5. Android开发(二十七)——android:layout_weight的真实含义

    android:layout_weight的真实含义是:一旦View设置了该属性(假设有效的情况下),那么该 View的宽度等于原有宽度(android:layout_width)加上剩余空间的占比! ...

  6. android:layout_weight的真实含义

    首先声明只有在Linearlayout中,该属性才有效.之所以android:layout_weight会引起争议, 是因为在设置该属性的同时,设置android:layout_width为wrap_ ...

  7. android:layout_weight属性的使用方法总结

    原创文章,转载请注明出处http://www.cnblogs.com/baipengzhan/p/6282826.html android:layout_weight属性可以和其他属性配合使用,产生多 ...

  8. Android LinearLayout的android:layout_weight属性

    本文主要介绍Android LinearLayout的android:layout_weight属性意义 android:layout_weight为大小权重,相当于在页面上显示的百分比,它的计算是根 ...

  9. Android学习之Layoutinflater的用法

    •她的第一次 话说,那是一个风雪交加的夜晚,看着她独自一个人走在漆黑的小道上,我抓紧跟了过去: 那晚,我们...... 记得第一次接触这个 Layoutinflater 应该是在学习 ListView ...

随机推荐

  1. IOS GCD

    UIButton * btn = [[UIButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; [btn setTitle:@&quo ...

  2. 用javascript 面向对象制作坦克大战(二)

    2.   完善地图 我们的地图中有空地,墙,钢,草丛,水,总部等障碍物. 我们可以把这些全部设计为对象. 2.1  创建障碍物对象群       对象群保存各种地图上的对象,我们通过对象的属性来判断对 ...

  3. HTML 表单提交 的简单代码

    <form action="check.php" method="post"> 用户名:<input type="text" ...

  4. 关于CCSprite改变box2d刚体位置以及角度。

    同事今天在讨论一个事情,box2d中,body不可以直接设置位置,这样是不合理的,因为在物理的世界,你去左右它的物理检测.它就没有存在的必要了.但是,有人就想直接用box2d的碰撞.不用物理模拟.怎么 ...

  5. [转]关于GCD与多线程

    GCD是什么,你知道吗?你知道了GCD,你确定你会使用吗? 这一篇文章是站在初学者角度去分析GCD,原因是这个很多iOS开发者根本就没用过,即使用过,不知道其中的原理.讲解之前认识一下什么是线程,为什 ...

  6. 【原】Storm学习资料推荐

    4.Storm学习资料推荐 书籍: 英文: Learning Storm: Ankit Jain, Anand Nalya: 9781783981328: Amazon.com: Books Gett ...

  7. 最短路+线段交 POJ 1556 好题

    // 最短路+线段交 POJ 1556 好题 // 题意:从(0,5)到(10,5)的最短距离,中间有n堵墙,每堵上有两扇门可以通过 // 思路:先存图.直接n^2来暴力,不好写.分成三部分,起点 终 ...

  8. IO-同步,异步,阻塞,非阻塞,阅读摘要

    http://www.cnblogs.com/Fly-Wind/p/io.html http://blog.csdn.net/historyasamirror/article/details/5778 ...

  9. 快速开发 jQuery 插件的 10 大技巧(转)

    转自:http://www.oschina.net/news/41776/jquery-10-tips 在开发过很多 jQuery 插件以后,我慢慢的摸索出了一套开发jQuery插件比较标准的结构和模 ...

  10. 【转】新建maven工程为什么jdk会是默认版本 而不是自己设置的版本?

    原文链接:为什么我eclipse新建项目的时候默认的是JRE1.5? 修改Maven中conf目录里的setting.xml文件内容,加上如下内容: <profiles> <!-- ...