最近在看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. hadoop(二)hadoop的安装部署

    系统版本 : 64位CentOS6.6 hadoop版本: 1.2.1 jdk版本: jdk1.6.0_45 环境准备 1.主机分配 主机名 ip master 1.0.0.0.10 slave1 1 ...

  2. LeetCode OJ-- Letter Combinations of a Phone Number ***

    https://oj.leetcode.com/problems/letter-combinations-of-a-phone-number/ 使用递归,深搜,使用 map 保存已经处理过的结果 cl ...

  3. PowerDesigner中如何生成主键和自增列

    1.SQL Server版本: 第一步,首先要建立与数据库的连接,方法较多,这里举个例子: http://www.cnblogs.com/netsql/archive/2010/05/17/17375 ...

  4. 洛谷 P1865 A % B Problem[筛素数/前缀和思想/区间质数个数]

    题目描述 区间质数个数 输入输出格式 输入格式: 一行两个整数 询问次数n,范围m 接下来n行,每行两个整数 l,r 表示区间 输出格式: 对于每次询问输出个数 t,如l或r∉[1,m]输出 Cros ...

  5. 【WEB基础】HTML & CSS 基础入门(8)表单

    前面 前面我们已经熟悉了网页上一些常见的元素,如在网页上显示一段文字.一张图片.一个列表.一张表格等等.这些东西都是事先编辑好显示在页面上只提供给用户看的,实际上,我们可以把这样的页面称之为静态页面. ...

  6. Java微信开发以及对各种云的评价

    目前一个人用Java开发一个微信的会员系统,开发已经结束,现在进入测试阶段. 有一些时间看看市面上的一些Java的微信开发视频,看了一下北风网的<微信公众平台开发Java版第一季>中的1, ...

  7. winform Loading效果

    做winform项目时,有可能用到异步耗时加载数据啥的,这个时候就需要我们封装一个正在加载的效果.下面是实现思路: 步骤一:把当前form界面使用句柄的方式截图出一个图片,这个图片会在下面用到,使用句 ...

  8. 【GLSL教程】(六)逐顶点的光照 【转】

    引言 在OpenGL中有三种类型的光:方向光(directional).点光(point).聚光(spotlight).本教程将从方向光讲起,首先我们将使用GLSL来模仿OpenGL中的光. 我们将向 ...

  9. [Android实例] Scroll原理-附ScrollView源码分析 (转载)

    想象一下你拿着放大镜贴很近的看一副巨大的清明上河图, 那放大镜里可以看到的内容是很有限的, 而随着放大镜的上下左右移动,就可以看到不同的内容了 android中手机屏幕就相当于这个放大镜, 而看到的内 ...

  10. Java中对象、对象引用、堆、栈、值传递以及引用传递的详解

    Java中对象.对象引用.堆.栈.值传递以及引用传递的详解 1.对象和对象引用的差别: (1).对象: 万物皆对象.对象是类的实例. 在Java中new是用来在堆上创建对象用的. 一个对象能够被多个引 ...