最近在看android的东西,发现很多和web前台的东西一样(思想)。只是看到很多属性的写法和前台有差别,刚刚看到这样的属性:

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的更多相关文章

  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中通过反射来设置Toast的显示时间

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

  4. 如何理解Android中的xmlns

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

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

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

  6. android中的数据库操作

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

  7. Android中常见的MVC模式

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

  8. Android中基于Socket的网络通信

    1. Socket介绍 2. ServerSocket的建立与使用 3. 使用ServerSocket建立聊天服务器-1 4. 使用ServerSocket建立聊天服务器-2 5. 在Android中 ...

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

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

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

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

随机推荐

  1. 学习环境配置:Manjaro、MSYS2以及常见软件

    0.前言 在说Manjaro之前,要先说一下Linux发行版.对于各大发行版而言,内核只有版本的差异,最重要的区别就是包管理系统.常见的包管理系统包括:Pacman,Apt , Yum和Portage ...

  2. MVC 二级联动 可以试试

    后台代码,获取数据如下: /// <summary> 2 /// 获取省份 3 /// </summary> 4 public JsonResult GetProvinceli ...

  3. Linux每日一坑001

    centos6,7中网卡/etc/sysconfig/network-scripts/ifcfg-eth0的命名是有要求的,必须是ifcfg-开头.改网卡名的时候掉坑.

  4. Codeforces Gym100814 B.Unlucky Teacher (ACM International Collegiate Programming Contest, Egyptian Collegiate Programming Contest (2015) Arab Academy for Science and Technology)

    今日份的训练题解,今天写出来的题没有昨天多,可能是因为有些事吧... 这个题就是老师改卷子,忘带标准答案了,但是他改了一部分卷子,并且确定自己改的卷子没出错,他想从改过的卷子里把标准答案推出来. 因为 ...

  5. 解决WordPress中字符转义的问题

    wordpress中输入两个"--"的时候会识别成一个横杠,如果此时的文章中有代码在,那么其他人在复制粘贴的时候就没法使用,于是乎我寻找了三种有效的方法解决此问题! WordPre ...

  6. 【Kafka】《Kafka权威指南》——分区partition

    在上篇的例子里([Kafka]<Kafka权威指南>--写数据), ProducerRecord 对象包含了目标主题.键和值. Kafka 的消息是 一个个 键值对, ProducerRe ...

  7. Node.js自动化技术实现(Java)

    Node.js自动化测试框架(NodeTestFramework):

  8. 25. Spring Boot使用自定义的properties【从零开始学Spring Boot】

    转:http://blog.csdn.net/linxingliang/article/details/52069515 spring boot使用application.properties默认了很 ...

  9. 将App发布到WasLiberty的较稳妥方法

    1.将应用解压放到一个目录 具体步骤: 1.1 建立目录,假设应用包为app.war且和新建目录sp在同一目录下 #mkdir sp 1.2 将app.war 改名为app.zip,这是为了解压#mv ...

  10. [ACM] HDU 5024 Wang Xifeng&#39;s Little Plot (构造,枚举)

    Wang Xifeng's Little Plot Problem Description <Dream of the Red Chamber>(also <The Story of ...