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

    1. <?xml version="1.0" encoding="utf-8"?>
    2. <!--
    3. <LinearLayout>
    4. 线性版面配置,在这个标签中,所有元件都是按由上到下的排队排成的
    5. -->
    6. <LinearLayout
    7. xmlns:android="http://schemas.android.com/apk/res/android"
    8. android:orientation="vertical"
    9. android:layout_width="fill_parent"
    10. android:layout_height="fill_parent">
    11. <!-- android:orientation="vertical" 表示竖直方式对齐
    12. android:orientation="horizontal"表示水平方式对齐
    13. android:layout_width="fill_parent"定义当前视图在屏幕上 可以消费的宽度,fill_parent即填充整个屏幕。
    14. android:layout_height="wrap_content":随着文字栏位的不同 而改变这个视图的宽度或者高度。有点自动设置框度或者高度的意思
    15. layout_weight 用于给一个线性布局中的诸多视图的重要度赋值。
    16. 所有的视图都有一个layout_weight值,默认为零,意思是需要显示
    17. 多大的视图就占据多大的屏幕空 间。若赋一个高于零的值,则将父视
    18. 图中的可用空间分割,分割大小具体取决于每一个视图的layout_weight
    19. 值以及该值在当前屏幕布局的整体 layout_weight值和在其它视图屏幕布
    20. 局的layout_weight值中所占的比率而定。
    21. 举个例子:比如说我们在 水平方向上有一个文本标签和两个文本编辑元素。
    22. 该文本标签并无指定layout_weight值,所以它将占据需要提供的最少空间。
    23. 如果两个文本编辑元素每一个的layout_weight值都设置为1,则两者平分
    24. 在父视图布局剩余的宽度(因为我们声明这两者的重要度相等)。如果两个
    25. 文本编辑元素其中第一个的layout_weight值设置为1,而第二个的设置为2,
    26. 则剩余空间的三分之二分给第一个,三分之一分给第二个(数值越小,重要
    27. 度越高)。
    28. -->
    29. <LinearLayout
    30. android:orientation="horizontal"
    31. android:layout_width="fill_parent"
    32. android:layout_height="fill_parent"
    33. android:layout_weight="1">
    34. <TextView
    35. android:text="red"
    36. android:gravity="center_horizontal"
    37. android:background="#aa0000"
    38. android:layout_width="wrap_content"
    39. android:layout_height="fill_parent"
    40. android:layout_weight="1"/>
    41. <TextView
    42. android:text="green"
    43. android:gravity="center_horizontal"
    44. android:background="#00aa00"
    45. android:layout_width="wrap_content"
    46. android:layout_height="fill_parent"
    47. android:layout_weight="1"/>
    48. <TextView
    49. android:text="blue"
    50. android:gravity="center_horizontal"
    51. android:background="#0000aa"
    52. android:layout_width="wrap_content"
    53. android:layout_height="fill_parent"
    54. android:layout_weight="1"/>
    55. <TextView
    56. android:text="yellow"
    57. android:gravity="center_horizontal"
    58. android:background="#aaaa00"
    59. android:layout_width="wrap_content"
    60. android:layout_height="fill_parent"
    61. android:layout_weight="1"/>
    62. </LinearLayout>
    63. <LinearLayout
    64. android:orientation="vertical"
    65. android:layout_width="fill_parent"
    66. android:layout_height="fill_parent"
    67. android:layout_weight="2">
    68. <TextView
    69. android:text="row one"
    70. android:textSize="15pt"
    71. android:layout_width="fill_parent"
    72. android:layout_height="wrap_content"
    73. android:layout_weight="1"/>
    74. <TextView
    75. android:text="row two"
    76. android:textSize="15pt"
    77. android:layout_width="fill_parent"
    78. android:layout_height="wrap_content"
    79. android:layout_weight="1"/>
    80. <TextView
    81. android:text="row three"
    82. android:textSize="15pt"
    83. android:layout_width="fill_parent"
    84. android:layout_height="wrap_content"
    85. android:layout_weight="1"/>
    86. <TextView
    87. android:text="row four"
    88. android:textSize="15pt"
    89. android:layout_width="fill_parent"
    90. android:layout_height="wrap_content"
    91. android:layout_weight="1"/>
    92. </LinearLayout>
    93. </LinearLayout>

[转]Android中的android:layout_width和android:width的更多相关文章

  1. android中shape的使用(android:angle小解)

    本文参考http://kofi1122.blog.51cto.com/2815761/521605和http://blog.csdn.net/qizi329/article/details/63098 ...

  2. android中少用静态变量(android静态变量static生命周期)

    在android中,要少用静态变量. 我现在做的一个应用中,之前的开发人员使用静态变量来存储cookie,这个全局的静态变量用来验证身份. 这时客户反应,应用长时间不使用,再次使用,会提示身份过期. ...

  3. android中的事件传递和处理机制

    一直以来,都被android中的事件传递和处理机制深深的困扰!今天特意来好好的探讨一下.现在的感觉是,只要你理解到位,其实事件的 传递和处理机制并没有想象中的那么难.总之,不要自己打击自己,要相信自己 ...

  4. android中的数据库操作

    如何在android中调用数据库资源 在android中主要有两种方法来实现对数据库的访问,一种是adb shell方式,另一种是通过相关的android 的java类来间接的对数据库来进行操作.其中 ...

  5. Android中常见的MVC模式

    MVC模式的简要介绍 MVC是三个单词的缩写,分别为: 模型(Model),视图(View)和控制Controller). MVC模式的目的就是实现Web系统的职能分工. Model层实现系统中的业务 ...

  6. Android中Dialog对话框的调用及监听

    Android中经常会需要在Android界面上弹出一些对话框提示用户,比如App的退出的时候都会有各种框来挽留你的心,支付宝的时候输入密码的密码框,非常常见及其实用的功能,类似于JS中的alter, ...

  7. Android中选项卡功能的实现

    Android中选项卡功能的实现 Android中使用TabHost和TabWidget来实现选项卡功能.TabHost必须是布局的根节点,它包含两个子节点: TabWidget,显示选项卡: Fra ...

  8. Android中AsyncTask使用具体解释

    在Android中我们能够通过Thread+Handler实现多线程通信.一种经典的使用场景是:在新线程中进行耗时操作.当任务完毕后通过Handler向主线程发送Message.这样主线程的Handl ...

  9. Android中通过反射来设置Toast的显示时间

    这个Toast的显示在Android中的用途还是非常大的,同一时候我们也知道toast显示的时间是不可控的.我们仅仅能改动他的显示样式和显示的位置,尽管他提供了一个显示时间的设置方法.可是那是没有效果 ...

  10. 如何理解Android中的xmlns

    转发自:https://www.jianshu.com/p/6fcaffaeffd2 作为一名 Android 开发,我想大家对xmlns并不会陌生,因为在写布局文件(如下代码所示)的时候经常会碰到, ...

随机推荐

  1. arch安装完成之后不能使用笔记本自带的无线网卡

    问题描述如下 我笔记本的wifi网卡识别不了,不知道为什么?? 使用ifconfig -a 只列出了有线网卡以及外接的无线网卡如下 enp4s0f1: flags=<UP,BROADCAST,M ...

  2. 2018 黑盾杯部分writeup

    这次比赛拿了三十多名.呵呵.或许这就是菜吧. 比赛经验教训: 1.赛前一定要调整好情绪. 2.比赛尽量不要紧张,每一题都认认真真的看过去! 3.站在出题者的角度去思考问题 4.刷题 5.还是情绪吧.其 ...

  3. 基于scrapy的分布式爬虫抓取新浪微博个人信息和微博内容存入MySQL

    为了学习机器学习深度学习和文本挖掘方面的知识,需要获取一定的数据,新浪微博的大量数据可以作为此次研究历程的对象 一.环境准备   python 2.7  scrapy框架的部署(可以查看上一篇博客的简 ...

  4. Linux搭建svn服务

    svn是为了方便代码进行版本控制 Linux)svn服务器 --> windows) svn访问端 ********* [root@svn ~]# yum install -y subversi ...

  5. 虚拟机NAT网络设置

    1. 虚拟机设置 2. 本地网络设置 3. 本地虚拟网卡设置 4. 安装虚拟机,设置网络为NAT方式即可访问外网.

  6. The hub and spoke model 轮辐模型/辐射模型

    最近一些文档中提到The Hub and Spoke Model,这里mark一下.hub表示轮毂,spoke表示轮辐,轮辐模型是简化网络路由的一套中心化的体系,广泛应用于航空.货运.快递以及网络技术 ...

  7. 总结开发ERP软件应遵循的一些基本原则

    总结一下做管理软件,有哪些项是经过检验的条款,必须遵守的. 界面篇 1  要保存用户的偏号(profile/favourite). ASP.NET 2.0引入此功能,当用户修改默认的控件的属性时,框架 ...

  8. django rest_framework比较完整的自定义实现样例

    里面有自定义的更新策略, 序列化时,考虑nest及显示. 很有参考意义. 然后,前端,可以考虑用angular.js或vue.js实现. 每次以token进行认证. url.py router = D ...

  9. lr11_Vugen_Genrial Options选项介绍:

    lr11_Vugen_Genrial Options选项介绍:

  10. Codeforces Round #222 (Div. 1) 博弈 + dp

    一般这种要倒着来. #include<bits/stdc++.h> #define LL long long #define fi first #define se second #def ...