Android XMl文件中tools前缀

平时在写资源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”

这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值