Android Studio tools attributes

Android Studio支持tools命名空间中的各种xml属性,这些属性支持设计时功能和编译时行为。当构建应用时,编译工具会移除这些属性,因此它们不会对APK大小或运行时行为产生影响。

要使用这些属性,需要将tools命名空间添加到您要在其中使用他们的各个 XML 文件的根元素中,如下所示:

<RootTag xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" >

PS:直接打出tools,然后使用自动提示功能可以自动添加tools命名空间。

错误处理属性


以下属性可以帮助抑制Lint警告信息。

tools:ignore
适用于:任何元素
使用者:Lint

此属性用于接受您希望工具针对相应元素或它的任何子元素忽略的 Lint 问题 ID 的逗号分隔列表。

例如,您可以告知工具忽略 MissingTranslation 错误:

<string name="show_all_apps" tools:ignore="MissingTranslation">All</string>

tools:targetApi
适用于:任何元素
使用者:Lint

此属性的工作方式与 Java 代码中的 @TargetApi 注解相同:通过它,您可以指定支持相应元素的 API 级别(指定为整数或代号)。

它会告知工具您认为该元素(及任何子元素)将只能在指定的 API 级别或更高级别中使用。这样,如果该元素或其属性在您指定为 minSdkVersion 的 API 级别中不可用,Lint 将不会向您发出警告。

例如,GridLayout 只能在 API 级别 14 及更高级别中使用,但您知道此布局不会用于任何更低版本,在这种情况下,您就可以使用此属性:

<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:targetApi="14" >

tools:locale
适用于:<resources>
使用者:Lint、Android Studio 编辑器

此属性用于告知工具用于给定 <resources> 元素中各项资源的默认语言/语言区域(因为工具会假设为英语),以免拼写检查工具发出警告。值必须是有效的语言区域限定符

例如,可以将此属性添加到 values/strings.xml 文件(默认字符串值)中,以指明用于默认字符串的语言。

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

设计时视图属性

以下属性定义了仅会在 Android Studio 布局预览中看到的布局特性。

tools: 代替 android:
适用于:<View>
使用者:Android Studio 布局编辑器

可以通过将 tools: 前缀(而不是 android:)与 Android 框架中的任意 <View> 属性搭配使用,在布局预览中插入示例数据。此属性在以下情况下很有用:在运行时之前,属性的值不会填充,但您希望提前在布局预览中看到效果。

例如,如果 android:text 属性值在运行时设置,或您希望在布局中看到默认值以外的值,则可以添加 tools:text,以便指定一些仅在布局预览中显示的文本。

也可以同时添加 android: 命名空间属性(在运行时使用)和匹配的 tools: 属性(会替换仅在布局预览中显示的运行时属性)。

还可以使用 tools: 属性来取消将某属性设置为仅用于布局预览。例如,如果拥有的 FrameLayout 包含多个子元素,但希望仅在布局预览中看到一个子元素,则可以将其中一个子元素设置为在布局预览中不可见,如下所示:

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="First" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Second"
        tools:visibility="invisible"  />
    

tools:context
适用于:任何根 <View>
使用者:Lint、Android Studio 布局编辑器

此属性用于声明相应布局默认与哪个 Activity 相关联。这会在编辑器或布局预览中启用具有以下特征的功能:需要了解相应 Activity 的信息,例如预览中的布局主题应该是什么,以及通过快速修复创建 onClick 处理程序时将其插入到什么位置。

可以使用清单文件中所用的同一点前缀指定 Activity 类名称(不包括完整的包名称)。

<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        tools:context=".MainActivity" >

tools:itemCount
适用于:<RecyclerView>
使用者:Android Studio 布局编辑器

对于给定的 RecyclerView,此属性会指定布局编辑器应该在 Preview 窗口中呈现的内容数量。

<android.support.v7.widget.RecyclerView
        android:id="@+id/recyclerView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:itemCount="3"/>
  

tools:layout
适用于:<fragment>
使用者:Android Studio 布局编辑器

此属性用于声明您希望布局预览在 Fragment 内绘制的布局(因为布局预览无法执行正常情况下会应用布局的 Activity 代码)。

<fragment android:name="com.example.master.ItemListFragment"
        tools:layout="@layout/list_content" />

tools:listitem / tools:listheader / tools:listfooter
适用于:<AdapterView>(以及 <ListView> 等子类)
使用者:Android Studio 布局编辑器

这些属性用于指定要在列表的项目、标题和页脚布局预览中显示的布局。布局中的任何数据字段都填充有数字内容(例如“Item 1”),以确保列表的项目不会重复。

<ListView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@android:id/list"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/sample_list_item"
        tools:listheader="@layout/sample_list_header"
        tools:listfooter="@layout/sample_list_footer" />
    

tools:showIn
适用于:<include> 引用的布局中的任何根 <View>
使用者:Android Studio 布局编辑器

通过此属性,您可以指向将相应布局用作内含布局的布局,以便在它出现并嵌入到其父级布局中时预览(和修改)此文件。

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:text="@string/hello_world"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:showIn="@layout/activity_main" />
    

现在当此 TextView 布局出现在 activity_main 布局内部时,布局预览中会进行显示。

tools:menu
适用于:任何根 <View>
使用者:Android Studio 布局编辑器

此属性用于指定布局预览应该在应用栏中显示的菜单。值可以是一个或多个菜单 ID,以英文逗号分隔(不带 @menu/ 或任何此类 ID 前缀,且不带 .xml 扩展名)。

<?xml version="1.0" encoding="utf-8"?>
    <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:menu="menu1,menu2" /> 

tools:minValue / tools:maxValue
适用于:<NumberPicker>
使用者:Android Studio 布局编辑器

这些属性用于设置 NumberPicker 视图的最小值和最大值。

<NumberPicker xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/numberPicker"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        tools:minValue="0"
        tools:maxValue="10" />

tools:openDrawer
适用于:<DrawerLayout>
使用者:Android Studio 布局编辑器

通过此属性,您可以在布局编辑器的 Preview 窗格中打开 DrawerLayout

<android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:openDrawer="start" />

"@tools:sample/*" 资源
适用于:支持界面文本或图片的任何视图。
使用者:Android Studio 布局编辑器

通过此属性,您可以将占位符数据或图片注入到视图中。例如,如果需要测试布局在采用文本时的行为,但尚未最终确定应用的界面文本,则可以使用占位符文本,如下所示:

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        tools:text="@tools:sample/lorem" />

资源压缩属性

通过以下属性,您可以启用严格引用检查,并在使用资源压缩时声明保留还是舍弃某些资源。

要启用资源压缩,请在 build.gradle 文件中将 shrinkResources 属性(以及适用于代码压缩的 minifyEnabled)设置为 true。例如:

android {
        ...
        buildTypes {
            release {
                shrinkResources true
                minifyEnabled true
                proguardFiles getDefaultProguardFile('proguard-android.txt'),
                        'proguard-rules.pro'
            }
        }
    }
    

 tools:shrinkMode
适用于:<resources>
使用者:具有资源压缩功能的编译工具

通过此属性,您可以指定编译工具应该使用“安全模式”(谨慎行事,保留所有明确引用的资源,以及可以通过调用 Resources.getIdentifier() 动态引用的资源)还是“严格模式”(仅保留代码或其他资源中明确引用的资源)。

默认使用“安全模式”(shrinkMode="safe")。要改为使用“严格模式”,请将 shrinkMode="strict" 添加到 <resources> 标记,如下所示:

<?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:tools="http://schemas.android.com/tools"
        tools:shrinkMode="strict" />
    

启用“严格模式”后,您可能需要使用 tools:keep 来保留已移除但您实际上需要的资源,并使用 tools:discard 来明确移除更多资源。

如需了解详情,请参阅压缩资源

tools:discard
适用于:<resources>
使用者:具有资源压缩功能的编译工具

使用资源压缩功能删除不使用的资源时,您可以通过此属性指定要手动舍弃的资源(通常情况下,原因是:相应资源被引用,但引用方式不会影响您的应用,或者 Gradle 插件错误地推导出相应资源被引用)。

要使用,请在资源目录中创建一个具有 <resources> 标记的 XML 文件(例如,在 res/raw/keep.xml 处),并将要保留在 tools:discard 属性中的各项资源指定为逗号分隔列表。您可以将星号字符用作通配符。例如:

<?xml version="1.0" encoding="utf-8"?>
    <resources xmlns:tools="http://schemas.android.com/tools"
        tools:discard="@layout/unused_1" />
发布了53 篇原创文章 · 获赞 17 · 访问量 3万+

猜你喜欢

转载自blog.csdn.net/jklwan/article/details/102877899