是时候让 Android Tools 属性拯救你了(1)

我们可以通过 tools:shrinkMode 和 tools:keep 属性来分别指定资源压缩的模式和需要保留的不被压缩的资源 ,还可以通过 tools:discard 属性来指定需要保留的资源,与 keep 功能类似:

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

<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:

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

<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”

app:layout_constraintEnd_toEndOf=“@+id/card_item_title”

app:layout_constraintHorizontal_bias=“0.0”

android:textSize=“12sp”

android:textColor=“@color/username_text_color”

android:layout_marginEnd=“16dp”

android:paddingEnd=“16dp”

tools:text=“水月沐风” />

<TextView

android:id=“@+id/card_item_title”

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:textSize=“16sp”

android:textColor=“@color/title_text_color”

app:layout_constraintStart_toEndOf=“@+id/card_item_avatar”

android:layout_marginStart=“12dp”

app:layout_constraintTop_toBottomOf=“@+id/card_item_username”

android:layout_marginTop=“8dp”

android:maxLines=“1”

tools:text=“今天上海的夜色真美!”/>

<TextView

android:id=“@+id/card_item_content”

android:layout_width=“0dp”

android:layout_height=“wrap_content”

app:layout_constraintStart_toStartOf=“parent”

android:layout_marginTop=“24dp”

app:layout_constraintTop_toBottomOf=“@+id/card_item_avatar”

app:layout_constraintBottom_toBottomOf=“parent”

android:layout_marginBottom=“16dp”

app:layout_constraintVertical_bias=“1.0”

android:maxLines=“3”

android:ellipsize=“end”

android:textColor=“@color/content_text_color”

android:textStyle=“normal”

app:layout_constraintEnd_toEndOf=“@+id/card_item_bottom_border”

android:layout_marginEnd=“16dp”

android:layout_marginStart=“16dp”

app:layout_constraintHorizontal_bias=“0.0”

tools:text=“人生若只如初见,何事秋风悲画扇…”/>

<ImageView

android:id=“@+id/card_item_poster”

android:layout_width=“0dp”

android:layout_height=“200dp”

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toBottomOf=“@+id/card_item_content”

app:layout_constraintEnd_toEndOf=“parent”

android:layout_marginEnd=“16dp”

android:scaleType=“centerCrop”

android:layout_marginTop=“8dp”

android:layout_marginStart=“16dp”

app:layout_constraintBottom_toBottomOf=“parent”

android:layout_marginBottom=“16dp”

app:layout_constraintVertical_bias=“0.0”

tools:ignore=“ContentDescription”

android:visibility=“visible”

tools:src=“@drawable/shanghai_night”/>

<View

android:id=“@+id/card_item_bottom_border”

android:layout_width=“0dp”

android:layout_height=“2dp”

app:layout_constraintTop_toBottomOf=“@+id/card_item_poster”

android:background=“#ffededfe”

app:layout_constraintEnd_toEndOf=“parent”

android:layout_marginTop=“16dp”

app:layout_constraintStart_toStartOf=“parent”/>

<TextView

android:layout_width=“wrap_content”

android:layout_height=“wrap_content”

android:id=“@+id/card_item_date”

android:layout_marginTop=“16dp”

app:layout_constraintTop_toTopOf=“parent”

app:layout_constraintEnd_toEndOf=“parent”

android:layout_marginEnd=“16dp”

android:textColor=“@color/date_text_color”

android:textSize=“12sp”

tools:text=“2019-08-10”/>

</android.support.constraint.ConstraintLayout>

通过上面代码我们可以发现:通过 对 TextView 使用 tools:text 属性代替 android:text 就可以实现文本具体效果的预览,然而这项设置并不会对我们 App 实际运行效果产生影响。同理,通过将 tools:src 作用于 ImageView 也可以达到预览图片的效果。此外。我们还可以对其他以 android: 为前缀的属性进行预览而不影响实际运行的效果,例如:上面布局代码中的底部分割线 <View>,我们想将其在 App 实际运行的时候隐藏掉,但我们还是需要知道它的预览效果和所占高度:

<View

android:id=“@+id/card_item_bottom_border”

android:layout_width=“0dp”

android:layout_height=“2dp”

android:visibility=“gone”

tools:visibility=“visible”

tools:layout_height=“8dp”

app:layout_constraintTop_toBottomOf=“@+id/card_item_poster”

android:background=“#ffededfe”

app:layout_constraintEnd_toEndOf=“parent”

android:layout_marginTop=“16dp”

app:layout_constraintStart_toStartOf=“parent”/>

如上所示,通过 tools:visibility 和 tools:layout_height 就可以仅在布局预览情况下改变 View 的状态和高度。虽然上述情况比较少用,但是希望大家也能够知道,tools: 可以替代所有 android: 修饰的属性。下面再列举一些其他会常用到的属性。

  • tools:layout

这个属性只能用于fragment 控件中,如果我们的 activity 布局文件中声明了 <fragment> 控件,我们就可以通过 tools:layout=”@layout/fragment_main” 来在当前 activity 布局中预览 fragment中的布局效果。

  • tools:showIn

这个属性就比较好玩了,它可以指定其他布局文件像 <include>组件一样在当前布局文件中使用和预览 <include> 控件的实际效果。例如,我们 card_item_layout.xml 作为 showIn 的对象给 show_in_layout.xml 布局使用,然后我就可以看到 show_in_layout.xml 中如下效果:

  • tools:menu

这个属性可以给当前布局预览器的 Toolbar 添加多个菜单项,但仅限于布局文件的根节点元素。如:

<?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:maxValue | tools:minValue

这两个属性仅用于 <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:listitem | tools:listheader | tools:listfooter | tools:listCount

下面来讲一下列表相关组件的 tools 属性。上面四个属性仅用于 <AdapterView>及其子类(如:ListView 和 RecyclerView)。然而,它们内部仍有一些使用限制:tools:listCount 仅用于RecyclerViewtools:listheader 和 tools:listfooter 仅限于 ListView;至于 tools:listitem 属性二者皆可用。之前的效果图就是借助于此属性:

activity_main.xml:

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

<android.support.constraint.ConstraintLayout

xmlns:android=“http://schemas.android.com/apk/res/android”

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

xmlns:app=“http://schemas.android.com/apk/res-auto”

android:layout_width=“match_parent”

android:layout_height=“match_parent”

tools:context=“.MainActivity”

android:background=“#ffEBEBEF”>

<android.support.v7.widget.Toolbar

android:id=“@+id/toolbar”

android:layout_width=“0dp”

android:layout_height=“wrap_content”

android:background=“?attr/colorPrimary”

android:theme=“?attr/actionBarTheme”

android:minHeight=“?attr/actionBarSize”

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintEnd_toEndOf=“parent”

android:elevation=“1dp”

app:title=“@string/app_name”

app:layout_constraintTop_toTopOf=“parent”

app:layout_constraintHorizontal_bias=“0.0”/>

<android.support.v7.widget.RecyclerView

android:layout_width=“match_parent”

android:layout_height=“wrap_content”

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintBottom_toBottomOf=“parent”

android:scrollbars=“vertical”

app:layout_constraintTop_toBottomOf=“@+id/toolbar”

app:layout_constraintHorizontal_bias=“0.0”

app:layout_constraintVertical_bias=“0.0”

tools:listitem=“@layout/card_item_layout”/>

</android.support.constraint.ConstraintLayout>

sample data


即 样本数据 功能,可以通过 @tools:sample 来使用该属性,也属于 design-time view attributes。但它并非只是一个属性那么简单,更应该算是一个“工具利器”,所以会将其单独拿出来详细介绍。这个工具是本年度 Google 大会上 Android 开发团队特别介绍的一个新推属性。它有什么用呢?用处大了!先前的布局预览使用的数据都是我们直接在布局控件中注明或者在 strings.xml 文件中给出的,这一就会产生一些脏数据,不利于我们后期的处理。而有了 sample data,我们就可以对布局预览器中的 “样本数据”进行集中保存和管理了。

1、sample data 的使用

Android studio 已为我们提供了以下样本数据,我可以直接拿来使用:

上述表格中不仅有常用文本数据和日期等数据,还提供了一些图片样本数据,那么该如何使用呢?很简单,只需要切换到布局预览界面,并拖动一个 ImageView 到面板上,然后 Android studio 就会弹出如下界面:

然后选择 avatars 或者 background/scenic 数据源就可以了。当然你也可以通过 xml 代码形式来设置:

<?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”

android:background=“@android:color/white”

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

android:layout_width=“match_parent”

android:layout_height=“match_parent”>

<ImageView

android:layout_width=“36dp”

android:layout_height=“36dp”

android:id=“@+id/imageView”

android:layout_marginTop=“16dp”

app:layout_constraintTop_toTopOf=“parent”

app:layout_constraintStart_toStartOf=“parent”

如何做好面试突击,规划学习方向?

面试题集可以帮助你查漏补缺,有方向有针对性的学习,为之后进大厂做准备。但是如果你仅仅是看一遍,而不去学习和深究。那么这份面试题对你的帮助会很有限。最终还是要靠资深技术水平说话。

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。建议先制定学习计划,根据学习计划把知识点关联起来,形成一个系统化的知识体系。

学习方向很容易规划,但是如果只通过碎片化的学习,对自己的提升是很慢的。

我搜集整理过这几年字节跳动,以及腾讯,阿里,华为,小米等公司的面试题,把面试的要求和技术点梳理成一份大而全的“ Android架构师”面试 Xmind(实际上比预期多花了不少精力),包含知识脉络 + 分支细节

img

在搭建这些技术框架的时候,还整理了系统的高级进阶教程,会比自己碎片化学习效果强太多。

网上学习 Android的资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。希望这份系统化的技术体系对大家有一个方向参考。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化学习资料的朋友,可以戳这里获取

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 21
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
README for preparing SD/MMC/Micro-SD CARD for booting Android Pre-Requesites -------------- 1) Need to have an SD/MMC/Micro-SD card with atleast 2GB of size. 2) The script needs to be invoked from ubuntu linux machine 8.04 or above. 3) User needs to have sudo privileges. What script will do ------------------- The mkmmc-android.sh partitions the MMC/SD card into three partiions namely boot, rootfs and data. The script will then put the boot images on boot partition and extracts the android rootfs-rootfs_*.tar.bz2 to rootfs partition. Finally the script will copy the Media clips to the data partition and START_HERE folder to boot partition. How to invoke the script ------------------------ There are three ways of invoking this script. 1) Command: sudo ./mkmmc-android <device> <MLO> <u-boot.bin> <uImage> <boot.scr> <rootfs tar.bz2 > <Media_Clips> <START_HERE Location> Example: sudo ./mkmmc-android /dev/sdc MLO u-boot.bin uImage boot.scr rootfs.tar.bz2 Media_Clips Details: In this case, the script will take Boot Images (MLO,u-boot.bin, uImage and boot.scr) Root Filesystem tarball, Media Clips and START_HERE folder as arguements. The script will then put the boot images on boot partition, extract the android rootfs-rootfs_*.tar.bz2 to rootfs partition. Finally the script will copy the Media clips to the data partition and START_HERE folder to boot partition. 2) Command: sudo ./mkmmc-android <device> <MLO> <u-boot.bin> <uImage> <boot.scr> <rootfs tar.bz2 > Example: sudo ./mkmmc-android /dev/sdc MLO u-boot.bin uImage boot.scr rootfs.tar.bz2 Details: In this case, the script will take Boot Images (MLO,u-boot.bin, uImage and boot.scr) and Root Filesystem tarball as arguements. The script will then put the boot images on boot partition, extract the android rootfs-rootfs_*.tar.bz2 to rootfs partition. The data partiton will be left empty in this case. 3) Command: sudo ./mkmmc-android.sh <device> Example: sudo ./mkmmc-android.sh /dev/sdc Details: In this case, the script will assume default locations for BootImages, Root Filesystem, Media_Clips and START_HERE. This command is equivalent to the following sudo ./mkmmc-android /dev/sdc Boot_Images/MLO Boot_Images/u-boot.bin Boot_Images/uImage Boot_Images/boot.scr Filesystem/rootfs.tar.bz2 Media_Clips START_HERE If you are in a particular Board Specific Directory, extracted from DevKit Release, then you can prepare the sd card by executing sudo ./mkmmc-android.sh <device>, to prepare sd card.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值