最近在看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. Linux 环境下安装配置 TigerVNC Server 并启用当前会话远程服务(X0VNC)

    曾经喜欢用 RealVNC Server 实现 Linux/Windows 的远程控制,因为 RealVNC 为收费商业软件,支持文件传输,性能优化方面也做得不错.但 RealVNC 从 5.0 版本 ...

  2. Java发送邮件----自己封装的方法

    发送邮件的封装类: package com.email; import java.util.Properties; import javax.mail.Authenticator; import ja ...

  3. Android 利用工具实现一键自动findViewById功能

    在线网站工具 地址:http://android.lineten.net/layout.php

  4. Android Touch事件传递机制详解 下

    尊重原创:http://blog.csdn.net/yuanzeyao/article/details/38025165 资源下载:http://download.csdn.net/detail/yu ...

  5. 转: RPC框架 远程对象服务引擎Hprose

    http://www.cnblogs.com/chenxizhang/archive/2010/07/18/1780258.html

  6. 转: IO设计模式:Reactor和Proactor对比

    转: https://segmentfault.com/a/1190000002715832 平时接触的开源产品如Redis.ACE,事件模型都使用的Reactor模式:而同样做事件处理的Proact ...

  7. 转: Syslog协议介绍

    转: http://liu-hliang.iteye.com/blog/827392 在网上搜的文章,写的很全乎.摘抄如下,供大家参考学习 1.介绍 在Unix类操作系统上,syslog广泛应用于系统 ...

  8. VC++动态链接库(DLL)编程深入浅出(二)

    好,让我们正式进入动态链接库的世界,先来看看最一般的DLL,即非MFC DLL 上节给大家介绍了静态链接库与库的调试与查看,本节主要介绍非MFC DLL. 4.非MFC DLL 4.1一个简单的DLL ...

  9. 红米note3刷安卓原生

    http://www.romzj.com/rom/63404.htm#comments-version 然后在系统设置里升级系统, http://www.lineageosdownloads.com/ ...

  10. vue2.X 自定义 模态框 modal

    1.自定义 modal Modal.vue <!-- 模态框 --> <template> <div class="modal-mask" v-sho ...