这个属性主要用在 <resource>
标签内,用来指定当前资源中默认为哪种语言和区域,进而规避语言拼写的检测,比如你可以指定values/strings.xml
文件的默认语言是西班牙语而不是英语:
<resources xmlns:tools=“http://schemas.android.com/tools”
tools:locale=“es”>
2、Resource shrinking attributes
即资源压缩属性。关于此类属性的用法我们已经在之前的一篇文章带你领略Android混淆的魅力
一文中做过详细讲解,下面再简单说明一下。
我们可以通过 tools:shrinkMode
和 tools:keep
属性来分别指定资源压缩的模式和需要保留的不被压缩的资源 ,还可以通过 tools:discard
属性来指定需要保留的资源,与 keep
功能类似:
<resources xmlns:tools=“http://schemas.android.com/tools”
tools:shrinkMode=“strict”
tools:keep=“@layout/activity_video*,@layout/dialog_update_v2”
tools:discard=“@layout/unused_layout,@drawable/unused_selector” />
下面就到本篇文章的重头戏了,注意,前方高能来袭!
3、Design-time View Attributes
这就是我们先前效果图中的重要功臣了,即:布局设计时的控件属性。这类属性主要作用于 View 控件,如上文所说的 tools:context
就是“成员”之一,下面我们来介绍其他重要成员。
在此之前,我们需要先揭开 tools
命名空间的另一层神秘面纱:tools:
可以替换任何以 android:
为前缀的属性,并为其设置样例数据(sample data
)。当然,正如我们前面所说,tools
属性只能在布局编辑期间有效,App真正运行后就毫无意义了,所以,我们就可以像下面这样来在运行前预览布局效果:
上图对应的布局文件为:
Card_item_layout.xml:
<android.support.constraint.ConstraintLayout
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
xmlns:android=“http://schemas.android.com/apk/res/android”
android:background=“@android:color/white”
android:clickable=“true”
android:focusable=“true”
android:foreground=“?attr/selectableItemBackground”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginStart=“2dp”
android:layout_marginEnd=“2dp”
tools:targetApi=“m”
tools:ignore=“UnusedAttribute”>
<ImageView
android:id=“@+id/card_item_avatar”
android:layout_width=“38dp”
android:layout_height=“38dp”
app:layout_constraintStart_toStartOf=“parent”
android:layout_marginStart=“16dp”
android:layout_marginTop=“16dp”
app:layout_constraintTop_toTopOf=“parent”
app:layout_constraintBottom_toBottomOf=“parent”
android:layout_marginBottom=“16dp”
app:layout_constraintVertical_bias=“0.0”
tools:ignore=“ContentDescription”
tools:srcCompat=“@drawable/user_other”/>
<TextView
android:id=“@+id/card_item_username”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“16dp”
app:layout_constraintTop_toTopOf=“parent”
app:layout_constraintStart_toStartOf=“@+id/card_item_title”