深入理解Android中的xmlns:tools属性

深入理解Android中的xmlns:tools属性,第1张

概述前言安卓开发中,在写布局代码的时候,ide可以看到布局的预览效果。但是有些效果则必须在运行之后才能看见,比如这种情况:TextView在xml中没有设置任何字符,而是在activity中设置了text。因此为了在ide中预览效

前言

安卓开发中,在写布局代码的时候,IDe可以看到布局的预览效果。

但是有些效果则必须在运行之后才能看见,比如这种情况:TextVIEw在xml中没有设置任何字符,而是在activity中设置了text。因此为了在IDe中预览效果,你必须在xml中为TextVIEw控件设置androID:text属性

@H_419_19@<TextVIEwandroID:ID="@+ID/text_main"androID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:textAppearance="@style/TextAppearance.Title"androID:layout_margin="@dimen/main_margin"androID:text="I am a Title" />

一般我们在这样做的时候都告诉自己,没关系,等写完代码我就把这些东西一并删了。但是你可能会忘,以至于在你的最终产品中也会有这样的代码。

用tools吧,别做傻事

以上的情况是可以避免的,我们使用tools命名空间以及其属性来解决这个问题。

@H_419_19@xmlns:tools=http://schemas.android.com/tools

tools可以告诉AndroID Studio,哪些属性在运行的时候是被忽略的,只在设计布局的时候有效。比如我们要让androID:text属性只在布局预览中有效可以这样

@H_419_19@<TextVIEwandroID:ID="@+ID/text_main"androID:layout_wIDth="match_parent"androID:layout_height="wrap_content"androID:textAppearance="@style/TextAppearance.Title"androID:layout_margin="@dimen/main_margin"tools:text="I am a Title" />

tools可以覆盖androID的所有标准属性,将androID:换成tools:即可。同时在运行的时候就连tools:本身都是被忽略的,不会被带进apk中。

tools属性的种类

tools属性可以分为两种:一种是影响lint提示的,一种是关于xml布局设计的。以上介绍的是tools的最基本用法:在UI设计的时候覆盖标准的androID属性,属于第二种。下面介绍lint相关的属性。

lint相关的属性

@H_419_19@tools:ignoretools:targetAPItools:locale

tools:ignore

ignore属性是告诉lint忽略xml中的某些警告。

假设我们有这样的一个ImageVIEw

@H_419_19@<ImageVIEwandroID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:layout_marginStart="@dimen/margin_main"androID:layout_margintop="@dimen/margin_main"androID:scaleType="center"androID:src="@drawable/divIDer" />

lint会提示该ImageVIEw缺少androID:contentDescription属性。我们可以使用tools:ignore来忽略这个警告:

@H_419_19@<ImageVIEwandroID:layout_wIDth="wrap_content"androID:layout_height="wrap_content"androID:layout_marginStart="@dimen/margin_main"androID:layout_margintop="@dimen/margin_main"androID:scaleType="center"androID:src="@drawable/divIDer"tools:ignore="contentDescription" />

tools:targetAPI

假设minSdkLevel 15,而你使用了API21中的控件比如rippledrawable

@H_419_19@<ripple xmlns:androID="http://schemas.androID.com/apk/res/androID"androID:color="@color/accent_color" />

则lint会提示警告。

为了不显示这个警告,可以:

@H_419_19@<ripple xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"androID:color="@color/accent_color"tools:targetAPI="LolliPOP" />

tools:locale(本地语言)属性

默认情况下res/values/strings.xml中的字符串会执行拼写检查,如果不是英语,会提示拼写错误,通过以下代码来告诉studio本地语言不是英语,就不会有提示了。

@H_419_19@<resourcesxmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"tools:locale="it"><!-- Your strings go here --></resources>

上面首先介绍了tools的最基本用法-覆盖androID的属性,然后介绍了忽略lint提示的属性。下面我们将继续介绍关于UI预览的其他属性(非androID标准属性)。

注意:关于忽略lint的属性,如果不想了解的话也没关系,因为并不影响编译,一般我都不会管这些警告。

这部分我们将继续介绍关于UI预览的其他属性(非androID标准属性)。

tools:context tools:menu tools:actionbarNavMode tools:Listitem/Listheader/Listfooter tools:showIn tools:layout

tools:context

context属性其实正是的称呼是activity属性,有了这个属性,IDe就知道在预览布局的时候该采用什么样的主题。同时他还可以在androID studio的java代码中帮助找到相关的文件(Go to Related files)

该属性的值是activity的完整包名

@H_419_19@<linearLayoutxmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"androID:ID="@+ID/container"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:orIEntation="vertical"tools:context="com.androID.example.MainActivity"> <!-- ... --></linearLayout>

tools:menu

告诉IDE 在预览窗口中使用哪个菜单,这个菜单将显示在layout的根节点上(actionbar的位置)。

其实预览窗口非常智能,如果布局和一个activity关联(指上面所讲的用tools:context关联)它将会自动查询相关activity的onCreateOptionsMenu方法中的代码,以显示菜单。而menu属性则可以覆盖这种默认的行为。

你还可以为menu属性定义多个菜单资源,不同的菜单资源之间用逗号隔开。

@H_419_19@tools:menu="menu_main,menu_edit"

如果你不希望在预览图中显示菜单则:

@H_419_19@tools:menu=""

最后需要注意,当主题为theme.AppCompat时,这个属性不起作用。

tools:actionbarNavMode

这个属性告诉IDe  app bar(Material中对actionbar的称呼)的显示模式,其值可以是

standardtabsList
@H_419_19@<linearLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"androID:orIEntation="vertical"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"tools:actionbarNavMode="tabs" />

同样的,当主题是theme.AppCompat (r21+,at least) 或者theme.Material,或者使用了布局包含Toolbar的方式。  该属性也不起作用,只有holo主题才有效。

Listitem,Listheader 和Listfooter 属性

顾名思义就是在ListVIEw ExpandableListVIEw等的预览效果中添加头部 尾部 以及子item的预览布局。

@H_419_19@<GrIDVIEwandroID:ID="@+ID/List"androID:layout_wIDth="match_parent"androID:layout_height="wrap_content"tools:Listheader="@layout/List_header"tools:Listitem="@layout/List_item"tools:Listfooter="@layout/List_footer" />

layout属性

tools:layout告诉IDe,Fragment在程序预览的时候该显示成什么样

@H_419_19@<fragment xmlns:androID="http://schemas.androID.com/apk/res/androID"xmlns:tools="http://schemas.androID.com/tools"androID:ID="@+ID/item_List"androID:name="com.example.fragmenttwopanel.ItemListFragment"androID:layout_wIDth="match_parent"androID:layout_height="match_parent"androID:layout_marginleft="16dp"androID:layout_marginRight="16dp"tools:layout="@androID:layout/List_content" />

tools:showIn

该属性设置于一个被其他布局<include>的布局的根元素上。这让您可以指向包含此布局的其中一个布局,在设计时这个被包含的布局会带着周围的外部布局被渲染。这将允许您“在上下文中”查看和编辑这个布局。需要 Studio 0.5.8 或更高版本。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

总结

以上是内存溢出为你收集整理的深入理解Android中的xmlns:tools属性全部内容,希望文章能够帮你解决深入理解Android中的xmlns:tools属性所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1147465.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-31
下一篇 2022-05-31

发表评论

登录后才能评论

评论列表(0条)

保存