一.@代表引用资源
1.引用自定义资源。格式:@[package:]type/name
android:text="@string/hello"
2.引用系统资源。格式:@android:type/name
android:textColor="@android:color/opaque_red"
注意:其实@android:type/name是@[package:]type/name 的一个子类
二.@*代表引用系统的非public资源。格式:@*android:type/name
系统资源定义分public和非public。public的声明在:
<sdk_path>\platforms\android-8\data\res\values\public.xml
@*android:type/name:可以调用系统定义的所有资源
@android:type/name:只能够调用publi属性的资源。
注意:没在public.xml中声明的资源是google不推荐使用的。
三.?代表引用主题属性
另外一种资源值允许你引用当前主题中的属性的值。这个属性值只能在style资源和XML属性中使用;它允许你通过将它们改变为当前主题提供的标准变化来改变UI元素的外观,而不是提供具体的值。例如:
android:textColor="?android:textDisabledColor"
注意,这和资源引用非常类似,除了我们使用一个"?"前缀代替了"@"。当你使用这个标记时,你就提供了属性资源的名称,它将会在主题中被查找,所以你不需要显示声明这个类型(如果声明,其形式就是?android:attr/android:textDisabledColor)。除了使用这个资源的标识符来查询主题中的值代替原始的资源,其命名语法和"@"形式一致:?[namespace:]type/name,这里类型可选。
四.@+代表在创建或引用资源 。格式:@+type/name
含义:”+”表示在R.java中名为type的内部类中添加一条记录。如"@+id/button"的含义是在R.java 文件中的id 这个静态内部类添加一条常量名为button。该常量就是该资源的标识符。如果标示符(包括系统资源)已经存在则表示引用该标示符。最常用的就是在定义资源ID中,例如:
@+id/资源ID名 新建一个资源ID
@id/资源ID名 应用现有已定义的资源ID,包括系统ID
@android:id/资源ID名 引用系统ID,其等效于@id/资源ID名
android:id="@+id/selectdlg"
android:id="@android:id/text1"
android:id="@id/button3"
五. android xmlns:tools作用以及用法 https://blog.csdn.net/qq_24531461/article/details/52804473
Android布局文件中的xmlns:tools作用以及用法
xmlns:tools="http://schemas.android.com/tools" tools工具
xmlns:cutsome="http://schemas.android.com/apk/res-auto" custome属性
布局文件中默认添加如此
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns 起什么作用
xmlns:android="http://schemas.android.com/apk/res/android 的作用是
这个是xml的命名空间,有了他,你就可以alt+/作为提示,提示你输入什么,不该输入什么,
什么是对的,什么是错的,也可以理解为语法文件。或者语法判断器什么的
这个主要作用是在运行的时候那些控件的属性都是通过它来识别的,如果上面你写错了,
不会有任何问题,但是在运行的时候就会有问题,提示你没有指定宽度等什么。这个是不用联网的
其中tools起什么作用呢?
可以参考文档http://tools.android.com/tech-docs/tools-attributes
在该文档中列举了很多tools的方法,比如tools:ignore/tools:targetApi/tools:locale等方法,
但是我们今天要讨论的则是该文档中最后面提到的内容Designtime Attributes,
文档地址为http://tools.android.com/tips/layout-designtime-attributes
在官方文档中有说这么一句: These are attributes which are used when the layout is rendered inthe tool, but have no impact on the runtime. This is useful if you for examplewant to put sample data in your textfields
for when you are editing the layout,but you don't want those attributes to affect your running app.
这些属性用于渲染布局,而不会影响到程序运行。也就是说只在预览布局时出现,在程序运行时相当于该属性不存在。
这个说明非常有意思,比如我们在布局文件中想要显示一段文字时,而该文字内容在程序中可能动态变化,特别是有参数的字符串内容%1$s之类的内容,以前必须使用android:text显示,然后调整这段文字的大小颜色参数,然后完成后删除android:text属性。有了tools参数后,可以直接使用tools:text在预览时显示文字即可,省却了上面删除的麻烦。
目前常用的有
tools:text,
tools:visibility,
tools.src,
tools.background
官方文档中也列出了一些限制,如下:
Currently only overriding existing attributes is supported. We may want to define some additional convenience attribute
s to make it simple to for example choose which child in a ViewFlipper to show etc. 当前只支持overriding已有的属性
You have to manually edit in your designtime attributes at this time
They do not appear as an option in for example the layout editor property sheet.
Editor code completion does not help you enter these attributes; the easiest way to use them is to first enter them in the Android namespace, and when done replacing the prefix. 目前还只能手动输入该部分,代码自动填充还无法完成
Note that designtime attributes are supported only for layout files themselves. You cannot use them anywhere else -- in menu XML files, in string resource definitions, etc.只支持layout布局,menu/resource/string暂时不支持,这个说法和http://tools.android.com/tech-docs/tools-attributes中内容有冲突,tools-attributes中提供了<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>以及<resources xmlns:tools="http://schemas.android.com/tools" tools:locale="es">内容
Designtime attributes can only be used for framework resources, not custom attributes at this point.只能适用于系统提供的属性,自定义属性不再这个范围
See https://code.google.com/p/android/issues/detail?id=46186 for background or additional requests or comments.
本文深入解读Android布局文件中资源引用的四种方式:自定义资源、系统资源、非公共系统资源及主题属性,并详细阐述了Android布局文件中xmlns:tools属性的作用与使用方法,包括其在预览布局时的功能特性,以及如何利用它简化布局设计过程。
5087

被折叠的 条评论
为什么被折叠?



