博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
android tools命名空间中好用的几个属性
阅读量:2085 次
发布时间:2019-04-29

本文共 1912 字,大约阅读时间需要 6 分钟。

一、tools 命名空间的作用有哪些?

根据官方文档描述,根据其属性的功能类别,大致有三种主要功能:

  • xml中的错误处理
  • xml 预览
  • 资源压缩

说的通俗一点就是:

  • 减少或者避免黄线提示,让代码更清爽,让编译少报错
  • 让预览界面更灵活,可以随心所欲的定制预览视图
  • 压缩资源文件,降低APK体积。

注意:用tools设置的默认值,只在预览时有用,不会影响运行时。

 

二、如何使用?

在布局的XML文件的根元素下,添加这条语句:xmlns:tools="http://schemas.android.com/tools"

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:cube_ptr="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#FFFFFF"

android:orientation="vertical"

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

tools:context=".MainActivity">

</RelativeLayout>

 

三、好用的几个属性:

1、tools:context

在布局文件中设置了tools:context,能够快速找到关联的activity。

这个属性通常设置在布局的XML文件的根元素,用来记录这个布局和哪个Activity相关。

这样在布局文件中,按住Ctrl+点击鼠标左键,可以定位到相关的Activity。

 

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:cube_ptr="http://schemas.android.com/apk/res-auto"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:background="#FFFFFF"

android:orientation="vertical"

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

tools:context=".MainActivity">

</RelativeLayout>

 

2、tools:listitem

在GridView/ListView/RecycleView中设置了tools:listitem,能够快速找到这个列表控件对应的item样式。

<GridView

android:id="@+id/gridview"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:horizontalSpacing="@dimen/dp_21"

android:numColumns="5"

android:paddingLeft="@dimen/dp_39"

android:paddingRight="@dimen/dp_39"

android:paddingTop="@dimen/dp_43"

android:verticalSpacing="@dimen/dp_23"

tools:listitem="@layout/main_grid_item"/>

 

3、tools:text

在TextView/Button等控件中设置了tools:text,能够起到假数据预览效果。真正运行时,设置的文字不会显示在控件上。

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

tools:text="hahaha" />

注意:若tools:text 和 android:text 同时存在,在预览时会优先展示 tools:text。

 

参考资料链接:

 

 

转载地址:http://mpsqf.baihongyu.com/

你可能感兴趣的文章
分布式——缓存一致性(Redis、MySQL)
查看>>
Gminer 配置参数
查看>>
Linux学习笔记——20170802
查看>>
Linux学习笔记——20170803
查看>>
Linux学习笔记——20170804
查看>>
Linux学习笔记——20170805
查看>>
Linux学习笔记——20170807
查看>>
MySQL学习笔记——20170808
查看>>
MySQL学习笔记——20170809
查看>>
MySQL学习笔记——20170810
查看>>
MySQL学习笔记——20170811
查看>>
MySQL学习笔记——20170812
查看>>
Android内存溢出与优化(五)——防止static引用
查看>>
Scala学习笔记——20170817
查看>>
Scala学习笔记——20170818
查看>>
Scala学习笔记——20170819
查看>>
Scala学习笔记——20170820
查看>>
Python学习笔记——20170821
查看>>
Python学习笔记——20170822
查看>>
Python学习笔记——20170823
查看>>