android xmlns:tools

本文详细介绍了Android开发中XML文件的tools属性,包括如何使用这些属性来预览布局、忽略警告、设置上下文等,帮助开发者提高开发效率。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

来源:http://tools.android.com/tech-docs/tools-attributes

xmlns:tools让android开发工具可以在xml文件中记录信息,同时这些信息不会被编译进安装文件中。其主要作用是预览和忽略一些检查。

命名空间的uri是xmlns:tools="http://schemas.android.com/tools",属性都是以tools开头。

tools:ignore

忽略属性检测。
这个属性可以在所有的xml中使用

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

tools:targetApi

忽略版本预警。
根java类里面使用@TargetApi一个作用, 可以用版本名也可以用版本id

<GridLayout tools:targetApi="ICE_CREAM_SANDWICH" >

GridLayout 在api14中引入的,如果程序的minsdk设置小于14,则会预警。

tools:locale

忽略字母拼写检测。
一般在资源文件的根标签中使用,可以设置语言或者时区,比如在values/strings.xml中设置

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

这样系统就知道,当前默认使用西班牙语而不是英语
ps:只在studio中可用

tools:context

关联activity,布局文件在预览时使用该activity的主题。
一般在布局文件的根标签中使用,

<android.support.v7.widget.GridLayout
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".MainActivity" ... >

tools:layout

对fragment绑定布局
专门针对fragment的属性,给fragment指定一个布局进行预览

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

tools:listitem / listheader / listfooter

对AdapterView的子view在预览中设置item,header,footer。

<ListView
    android:id="@android:id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:listitem="@android:layout/simple_list_item_2"
    tools:listheader="@layout/list_header"
    tools:listfooter="@layout/list_footer" />

ps:没有实验成功,不知道为啥。

tools:showIn

如果一个布局被其他布局使用<include>标签包含进去,可以在当前布局设置这个属性,这样在预览中就会显示整体的效果。

<?xml version="1.0" encoding="utf-8"?>
<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" />

activity_main里面include了这个布局文件。

tools:menu

设置菜单。
这个属性用在一个布局文件的根标签中,在预览中显示actionBar的菜单内容。如果设置了tools:context属性,则studio将会通过绑定的activity的onCreateOptionsMenu()来获取菜单内容。菜单内容以逗号分隔,同时可以使用menu的资源文件。

<?xml version="1.0" encoding="utf-8"?>
<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" />

tools:actionBarNavMode

设置actionBar的导航模式。
这个属性用在布局的根标签中。有三种模式: “standard”, “list” and “tabs

<?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:actionBarNavMode="tabs" />

PS:只能在studio中使用

tools:shrinkMode,tools:keep,tools:discard

这三个属性是资源保留移除相关的,详细介绍参见

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值