[转]Android中的android:layout_width和android:width
android:width 其实是定义控件上面的文本(TextView) 的宽度,当然这个宽度也是和 android:layout_width 配合起来作用的,如果 android:layout_width="fill_parent" 的话,那么设置 android:width 是没有意义的
android:layout_width 其实是可以实现 android:width 的效果的,我觉得这应该是为什么在 android 实例中看不到有人用 android:width 的原因吧。
若还要讲讲两者的区别的话,那就是:
android:width 的值,一般是 "100dp" 这样的数值;
android:layout_width 的值,一般是"fill_parent","wrap_content","match_parent".当然,它也可以像前者一样,设置数值的.
带"layout"的属性是指整个控件而言的,是与父控件之间的关系,如 layout_gravity 在父控件中的对齐方式, layout_margin 是级别相同的控件之间的间隙等等;
不带"layout" 的属性是指控件中文本的格式,如gravity是指文本的对齐方式等等,而其中文本的格式又受制约于它的控件在父控件中的属性.
借用一位大牛的示例:http://zhangcong170.iteye.com/blog/423173
- <?xml version="1.0" encoding="utf-8"?>
- <!--
- <LinearLayout>
- 线性版面配置,在这个标签中,所有元件都是按由上到下的排队排成的
- -->
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent">
- <!-- android:orientation="vertical" 表示竖直方式对齐
- android:orientation="horizontal"表示水平方式对齐
- android:layout_width="fill_parent"定义当前视图在屏幕上 可以消费的宽度,fill_parent即填充整个屏幕。
- android:layout_height="wrap_content":随着文字栏位的不同 而改变这个视图的宽度或者高度。有点自动设置框度或者高度的意思
- layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。
- 所有的视图都有一个layout_weight值,默认为零,意思是需要显示
- 多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视
- 图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight
- 值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布
- 局的layout_weight值中所占的比率而定。
- 举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。
- 该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。
- 如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分
- 在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个
- 文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,
- 则剩余空间的三分之二分给第一个,三分之一分给第二个(数值越小,重要
- 度越高)。
- -->
- <LinearLayout
- android:orientation="horizontal"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="1">
- <TextView
- android:text="red"
- android:gravity="center_horizontal"
- android:background="#aa0000"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"/>
- <TextView
- android:text="green"
- android:gravity="center_horizontal"
- android:background="#00aa00"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"/>
- <TextView
- android:text="blue"
- android:gravity="center_horizontal"
- android:background="#0000aa"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"/>
- <TextView
- android:text="yellow"
- android:gravity="center_horizontal"
- android:background="#aaaa00"
- android:layout_width="wrap_content"
- android:layout_height="fill_parent"
- android:layout_weight="1"/>
- </LinearLayout>
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:layout_weight="2">
- <TextView
- android:text="row one"
- android:textSize="15pt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"/>
- <TextView
- android:text="row two"
- android:textSize="15pt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"/>
- <TextView
- android:text="row three"
- android:textSize="15pt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"/>
- <TextView
- android:text="row four"
- android:textSize="15pt"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:layout_weight="1"/>
- </LinearLayout>
- </LinearLayout>
[转]Android中的android:layout_width和android:width的更多相关文章
- android中shape的使用(android:angle小解)
本文参考http://kofi1122.blog.51cto.com/2815761/521605和http://blog.csdn.net/qizi329/article/details/63098 ...
- android中少用静态变量(android静态变量static生命周期)
在android中,要少用静态变量. 我现在做的一个应用中,之前的开发人员使用静态变量来存储cookie,这个全局的静态变量用来验证身份. 这时客户反应,应用长时间不使用,再次使用,会提示身份过期. ...
- android中的事件传递和处理机制
一直以来,都被android中的事件传递和处理机制深深的困扰!今天特意来好好的探讨一下.现在的感觉是,只要你理解到位,其实事件的 传递和处理机制并没有想象中的那么难.总之,不要自己打击自己,要相信自己 ...
- android中的数据库操作
如何在android中调用数据库资源 在android中主要有两种方法来实现对数据库的访问,一种是adb shell方式,另一种是通过相关的android 的java类来间接的对数据库来进行操作.其中 ...
- Android中常见的MVC模式
MVC模式的简要介绍 MVC是三个单词的缩写,分别为: 模型(Model),视图(View)和控制Controller). MVC模式的目的就是实现Web系统的职能分工. Model层实现系统中的业务 ...
- Android中Dialog对话框的调用及监听
Android中经常会需要在Android界面上弹出一些对话框提示用户,比如App的退出的时候都会有各种框来挽留你的心,支付宝的时候输入密码的密码框,非常常见及其实用的功能,类似于JS中的alter, ...
- Android中选项卡功能的实现
Android中选项卡功能的实现 Android中使用TabHost和TabWidget来实现选项卡功能.TabHost必须是布局的根节点,它包含两个子节点: TabWidget,显示选项卡: Fra ...
- Android中AsyncTask使用具体解释
在Android中我们能够通过Thread+Handler实现多线程通信.一种经典的使用场景是:在新线程中进行耗时操作.当任务完毕后通过Handler向主线程发送Message.这样主线程的Handl ...
- Android中通过反射来设置Toast的显示时间
这个Toast的显示在Android中的用途还是非常大的,同一时候我们也知道toast显示的时间是不可控的.我们仅仅能改动他的显示样式和显示的位置,尽管他提供了一个显示时间的设置方法.可是那是没有效果 ...
- 如何理解Android中的xmlns
转发自:https://www.jianshu.com/p/6fcaffaeffd2 作为一名 Android 开发,我想大家对xmlns并不会陌生,因为在写布局文件(如下代码所示)的时候经常会碰到, ...
随机推荐
- 一键切图 PS 动作 【收藏】
使用方法 一键切图动作.zip 1. 下载动作 2. 打开PS 动作 窗口,导入动作 3. 选中图层后 点击 F2 一键切图 详情看原文链接 原文链接
- PHP 结合实例认识 Socket
一.简介 (本部分参考自 揭开Socket的神秘面纱 - 源码工作室) 请结合下图 Socket 抽象层的位置来理解.Socket 是应用层和 TCP/IP 协议族进行通信的中间软件抽象层,是一组接口 ...
- linux 内核信号量
Linux内核的信号量在概念和原理上和用户态的System V的IPC机制信号量是相同的,不过他绝不可能在内核之外使用,因此他和System V的IPC机制信号量毫不相干. 信号量在创建时需要设置一个 ...
- 浅析linux内核中timer定时器的生成和sofirq软中断调用流程(转自http://blog.chinaunix.net/uid-20564848-id-73480.html)
浅析linux内核中timer定时器的生成和sofirq软中断调用流程 mod_timer添加的定时器timer在内核的软中断中发生调用,__run_timers会spin_lock_irq(& ...
- Linux命令参数处理 shell脚本函数getopts
getopts 命令 用途 处理命令行参数,并校验有效选项. 语法 getopts 选项字符串 名称 [ 参数 ...] 描述 getopts 的设计目标是在循环中运行,每次执行循环,getopts ...
- Gradient-Based Learning Applied to Document Recognition 部分阅读
卷积网络 卷积网络用三种结构来确保移位.尺度和旋转不变:局部感知野.权值共享和时间或空间降采样.典型的leNet-5如下图所示: C1中每个特征图的每个单元和输入的25个点相连,这个5* ...
- HDU 1285 确定比赛名次(拓扑排序模板)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1285 题目大意:有N个比赛队(1<=N<=500),编号依次为1,2,3,....,N进行 ...
- gentoo emerge unable to sync
gentoo emerge unable to sync Author: Tubo After setting SYNC to customized URL: SYNC="rsync://m ...
- anaconda不错的
- vim 图册
网上看到的一些图,感觉不错,分享一下 我现在感觉配置文件,很多没有必要,反而很花哨,但是这些基础的东西,反而很高效,实在 VIM的列编辑操作 删除列 1.光标定位到要操作的地方. 2.CTRL+v 进 ...