您的位置:首页 > 其它

你是不是也忽略了xml里面的tools命名空间

2016-05-18 18:04 351 查看
一创建一个布局文件的时候可能都有这么一句:

xmlns:tools="http://schemas.android.com/tools"


基础认识

可是这个tools的命名空间有什么属性有什么作用呢,相信很多人都忽略了它的存在。它主要用在项目开发阶段而不会影响用户体验,用在Design界面渲染而不会影响运行时的界面。

有时这些巧妙的属性会节约我们的构建时间。我并不是说会加快构建速度,而是构建相关的 UI 改变会减少。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"/>


当然这个tools你可以改成其它,只要他能代表这个命名空间就可以。该属性的所有属性都不会影响apk的大小或运行时,它们会在Gradle打包应用时被分离出去。

以前在做andorid开发时,为了直观的查看布局经常在TextView里面用android:text=”“来添加测试字符串,然而这样做就会让我们apk中包含没有用的资源和渲染成本。有这个tools我们可以用tools:text=”“,这样就会在设计渲染时显示出来而不在运行时显示。

我们可以同时使用android和tools命名空间。tools命名空间将会用在设计阶段而前者会用在运行时。

有时候你希望在运行时开启而有设计时关闭。Android文档展示了ListView的例子:

<ListView
android:id="@+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fastScrollAlwaysVisible="true"
tools:fastScrollAlwaysVisible=""/>


这里表示:有运行时开启了
fastScrollAlwaysVisible
功能,而在设计时关闭了它。

你可以覆盖所有已存在的android命名空间的属性,但无法覆盖自定义属性。

在xml中指定目标API版本

你可以在xml中指定API版本,就像在java代码中使用@TargetApi一样。API版本可以通过一个整形或它的代号指定。这可以用来避免特定XML属性的问题。比如:

<TextView
tools:text="Mastering ToolsNs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_height="match_parent"
tools:targetApi="M"
/>


告诉Lint你的字符串资源是正确的

由于AndroidStudio/Lint默认语言是英语,如果你有其它语言的字符串资源,它将会显示排版警告。

告知Lint你本地化资源的技巧:

<resources xmlns:tools="http://schemas.android.com/tools"
tools:locale="es"/>


这样做了之后就不会显示排版警告了。

在Fragment和自定义视图上预览布局

在使用Fragment和自定义视图时非常有用。通过
tools:layout="@layout/your_layout"
,你可以设置在预览窗口显示一个布局。

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
>

<fragment
android:name="com.alexsimo.mastertoolsnamespace.BooksFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="@layout/fragment_books"
/>

</LinearLayout>


上述代码使用了 tools:layout 属性来预览 BooksFragment 布局,而不用将工程运行在设备或模拟器上来查看效果。是不是觉得很方便呢?

关联xml到activity上

当我们用AndroidStudio创建一个acitivty的时候,在默认生成的xml文件中你会看到
tools:context=".MainActivity"
。单个xml布局可以用在多个activity或fragment中,使用些属性,你就告诉了AndroidStudio哪个activity与这个xml相关联。

当主题被定义在了AndroidManifest.xml文件之后这个帮助布局编辑器猜测这个activity的主题。

忽略LInt警告

忽略Lint警告不是一个好主意,如果Lint上报问题,你应该行动起来并修复错误和警告,但有时Lint给出的是错误警告,我们明确知道(may be not)我们在做什么,这种情况下你可以用使用
tool:ignore=""


如果我们的外层布局没有存在的意义的时候:

<LinearLayout
android:id="@+id/fullscreen_content_controls"
style="?buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:background="@color/black_overlay"
android:orientation="horizontal"
tools:ignore="UselessParent" >

<Button
android:id="@+id/dummy_button"
style="?buttonBarButtonStyle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="@string/dummy_button" />
</LinearLayout>


可以用
tools:ignore="UselessParent"
来忽略警告。

带菜单预览布局

默认情况下,定义在 Activity.onCreateOptionsMenu() 中的菜单会被渲染到预览窗口。但你可以使用 tools:menu=”comma separated menu IDs” 覆盖此菜单。我个人不会使用该属性,但它可能会对你有用。

设置 Toolbar 导航模式

此属性很简单!使用 tools:actionBarNavMode=”standard|list|tabs” 你可以设置 ActivityBar 的导航模式。

Shrink resources(收缩资源)

Android tools 命名空间中有许多关于收缩资源的属性,比如 tools:shrinkMode=”strict|safe”,tools:keep=”@layout|layout_wildcard”,tools:discard=”@layout/unused” 等,但我不准备在此讨论它们,因为本文不是讨论收缩资源的,如果你感兴趣,可以在 Android Studio 官方文档中了解更多信息。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: