【UI】布局tools:xxx属性的妙用

XML 文件的根元素

xmlns:tools=“http://schemas.android.com/tools”

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

属性列表如下

错误处理属性

  1. tools:ignore
  2. tools:targetApi
  3. tools:locale

设计时视图属性

  1. tools:context
  2. tools:itemCount—RecyclerView
  3. tools:layout—fragment
  4. tools:listitem—AdapterView
  5. tools:listheader—AdapterView
  6. tools:listfooter—AdapterView
  7. tools:showIn
  8. tools:menu
  9. tools:minValue、tools:maxValue—NumberPicker
  10. tools:openDrawer—DrawerLayout
  11. @tools:sample/*
  12. tools: 代替 android:tools:text、tools:visibility

资源压缩属性

  • tools:shrinkMode
  • tools:keep
  • tools:discard

tools:ignore

以下属性可帮助抑制 Lint 警告消息。
适用于:任何元素

使用者: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" >

但是,您应该使用支持库中的 GridLayout

tools:locale

适用于:

使用者:Lint、Android Studio 编辑器

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

例如,您可以将此属性添加到 values/strings.xml 文件(默认字符串值)中,以指明用于默认字符串的语言是西班牙语,而不是英语

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

tools:context

适用于:任何根

使用者:lint、Android Studio 布局编辑器

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

只有在您设置了 tools:context 时,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

适用于:

使用者: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

适用于:

使用者:Android Studio 布局编辑器

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

例如:

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

tools:listitem | tools:listheader | tools:listfooter适用于:(以及 等子类)

使用者: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

适用于: 引用的布局中的任何根

使用者: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

适用于:任何根

使用者: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

适用于:

使用者: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

适用于:

使用者:Android Studio 布局编辑器

通过此属性,您可以在布局编辑器的 Preview 窗格中打开 DrawerLayout。您还可以通过传递以下任一值修改布局编辑器呈现布局的方式:

常量		值		说明
end		800005	将对象推送到其容器的结尾,不更改其大小。
left	3		将对象推送到其容器的左侧,不更改其大小。
right	5		将对象推送到其容器的右侧,不更改其大小。
start	800003	将对象推送到其容器的开头,不更改其大小。
<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" />

下表列出了可以注入到布局中的占位符数据的类型。

属性值							占位符数据说明
@tools:sample/full_names		从 @tools:sample/first_names 和 @tools:sample/last_names 		的组合中随机生成的全名。
@tools:sample/first_names		常见名字。
@tools:sample/last_names		常见姓氏。
@tools:sample/cities			世界各地的城市名称。
@tools:sample/us_zipcodes		随机生成的美国邮政编码。
@tools:sample/us_phones			随机生成的电话号码,格式如下:(800) 555-xxxx。
@tools:sample/lorem				源自拉丁语的占位符文本。
@tools:sample/date/day_of_week	指定格式的随机日期和时间。
@tools:sample/date/ddmmyy
@tools:sample/date/mmddyy
@tools:sample/date/hhmm
@tools:sample/date/hhmmss
@tools:sample/avatars				可用作个人资料头像的矢量可绘制对象。
@tools:sample/backgrounds/scenic	可用作背景的图片。

tools: 代替 android

适用于:

使用者:Android Studio 布局编辑器

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

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

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ToolsActivity">


    <Button

        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        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"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@id/button"
        />

</android.support.constraint.ConstraintLayout>

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

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

在这里插入图片描述

在设计视图中使用布局编辑器时,您还可以通过属性窗口修改部分设计时视图属性。每个设计时属性都用属性名称旁边的扳手图标 进行指示,以便与同名的真实属性区分开。

资源压缩属性

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

要启用资源缩减功能,请将 build.gradle 文件中的 shrinkResources 属性(若为代码缩减,则还包括 minifyEnabled)设置为 true。例如:

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

tools:shrinkMode

适用于:

使用者:具有资源压缩功能的构建工具

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

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

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

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

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

tools:keep

适用于:

使用者:具有资源压缩功能的构建工具

使用资源压缩功能移除不使用的资源时,您可以通过此属性指定要保留的资源(通常情况下,原因是:相应资源在运行时以间接方式引用,例如通过将动态生成的资源名称传递给 Resources.getIdentifier())。

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

<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:tools="http://schemas.android.com/tools"
    tools:keep="@layout/used_1,@layout/used_2,@layout/*_3" />

tools:discard

适用于:

使用者:具有资源压缩功能的构建工具

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

要使用,请在资源目录中创建一个具有 标记的 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" />

自研产品推荐

历时一年半多开发终于smartApi-v1.0.0版本在2023-09-15晚十点正式上线
smartApi是一款对标国外的postman的api调试开发工具,由于开发人力就作者一个所以人力有限,因此v1.0.0版本功能进行精简,大功能项有:

  • api参数填写
  • api请求响应数据展示
  • PDF形式的分享文档
  • Mock本地化解决方案
  • api列表数据本地化处理
  • 再加上UI方面的打磨

为了更好服务大家把之前的公众号和软件激活结合,如有疑问请大家反馈到公众号即可,下个版本30%以上的更新会来自公众号的反馈。
嗯!先解释不上服务端原因,API调试工具的绝大多数时候就是一个数据模型、数据处理、数据模型理解共识的问题解决工具,所以作者结合自己十多年开发使用的一些痛点来打造的,再加上服务端开发一般是面向企业的,作者目前没有精力和时间去打造企业服务。再加上没有资金投入所以服务端开发会滞后,至于什么时候会进行开发,这个要看募资情况和用户反馈综合考虑。虽然目前国内有些比较知名的api工具了,但作者使用后还是觉得和实际使用场景不符。如果有相关吐槽也可以在作者的公众号里反馈蛤!
下面是一段smartApi使用介绍:
在这里插入图片描述

下载地址:

https://pan.baidu.com/s/1iultkXqeLNG4_eNiefKTjQ?pwd=cnbl

posted @ 2022-02-09 09:50  lichong951  阅读(5)  评论(0编辑  收藏  举报  来源