平时在写资源XMl文件的时候,经常看到的前缀都是android:或者是tools:。
当然你在引用这两个前缀的时候需要导入其命名空间(Namespace)。
例如下面这个例子:
在该例子中我使用了android:前缀,但是未导入其命名空间,导致报错,只用加上这句话就可以恢复正确:
xmlns:android=”http://schemas.android.com/apk/res/android”
当然类似,如果你引用tools:前缀,那你也需要导入tools的命名空间:
xmlns:tools=”http://schemas.android.com/tools”
官网:http://tools.android.com/tech-docs/tools-attributes
http://tools.android.com/tips/layout-designtime-attributes
As of Android Studio 0.2.11, the layout rendering (used in both the layout editor as well as the XML editor layout preview window), supports designtime layout attributes.
These are attributes which are used when the layout is rendered in the tool, but have no impact on the runtime. This is useful if you for example want 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.
The tools namespace is a specially recognized namespace by the Android tools, so all the attributes you define on view elements in the tools-namespace will be automatically stripped when the application is packaged and there is no runtime overhead.
Usage:
- android: -> tools:
- Can both set Android namespace attribute and tools attribute
例如:
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First"
tools:visibility="invisible" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second"
tools:visibility="visible" />
Limitations:
- Can only overriding existing attributes
- No option and no code completion(u can replace the prefix)
- Only support framework resources, no custom attributes
Other Usage?
tools:ignore –- 告诉Lint忽略xml中的一些警告
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/divider" />
Warning: Missing contentDescription attribute on image.
Solution: tools:ignore=”contentDescription”
tools:targetApi – minSdkLevel:15, but u need the widgets in high API level
<ripple xmlns:android="http://schemas.android.com/apk/res/android"
android:color="@color/accent_color" />
Solution: tools:targetApi =“LOLLIPOP”
tools:locale – undo spell checking in res/values/strings.xml
<resources
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:locale="it">
<!-- Your strings go here -->
</resources>
tools:listitem;tools:listheader;tools:listfooter
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
tools:listheader="@layout/epaysdk_view_payments_header"
tools:listitem="@layout/epaysdk_item_payments_usable"
tools:listfooter="@layout/epaysdk_view_payments_footer"/>
tools:showIn
公共部分layout:titlebar.xml
引用公共部分的layout:paysms.xml、payshorty.xml、paypwd.xml
在公共部分最顶端的layout中指明其中一个被包含的layout:
tools:showIn=”@layout/frag_paysms”