如何将新的占位符资源放入Android Studio项目(“工具:示例”资源)?

时间:2017-09-13 08:26:25

标签: android android-studio

背景

有时,您希望将一些占位符仅显示在IDE上的布局文件中。

例如,您可以使用:

<ImageView tools:src="@tools:sample/avatars" ... />

在预览中得到这个:

enter image description here

此类文件不是您构建应用时获得的APK的一部分,因此仅用于开发是安全的。

这就是我从here得知的:

  

使用3.0中的示例数据,您现在可以拥有占位符图像   不是编译的apk的一部分。你只需要一个samplesata目录   您的项目包含一个包含您想要的所有图像的子目录   用作占位符。你可以从“工具”中引用这些图像   属性。此外,有预定义的股票图像   @ sample / avatars或@ sample / background / scenic

问题

我找不到如何在项目中添加更多这样的图像(这样它们只会在IDE中使用,而不是APK的一部分),如果有办法放置其他资源,除了图像。

实际上我找不到此功能的文档。

我尝试了什么

我试图将图像放在“res / sampledata”上并尝试“res / sample”,但在两种情况下都无法达到它。

问题

  1. 甚至这个功能的名称是什么?
  2. 如何将图像文件放入项目中,并以此方式将其用作占位符?在哪个文件夹?
  3. 是否可以添加更多图像以便这种方式使用?
  4. 是否可以添加其他资源?布局?字符串?
  5. 此新功能是否有更多功能?

4 个答案:

答案 0 :(得分:19)

  

甚至这个功能的名称是什么?

官方消息来源未经证实,但它可能被称为&#34;样本数据&#34;。

  

如何将图像文件放入项目中,并以此方式将其用作占位符?在哪个文件夹?

与图像,字体等资源不同。示例数据不会进入/res/(它们不会随应用程序一起编译。因此,通过将它们完全分开来过滤它们可能更容易目录)。他们进入/app/sampledata/,例如:/app/sampledata/image.png

您可以通过右键点击应用创建samplesata文件夹并执行New > Sample Data directory

enter image description here

然后您可以使用@sample/

像这样引用它们
<ImageView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    tools:src="@sample/test.png" />

虽然这没有给出任何错误,但遗憾的是这个功能现在似乎有问题,因为图像没有出现在预览中,无论它们是否放在子目录中(尝试过png,jpeg,jpg,xml)

有趣的是,将单个图像放在子目录中并引用该子目录而不是特定图像似乎有效:

的结构

enter image description here

结合这些参考资料

<?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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:background="@sample/image">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        tools:src="@sample/avatar" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        tools:src="@sample/jpeg" />

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="100dp"
        tools:src="@sample/vector" />

</LinearLayout>

制作此预览。请注意我是如何利用tools:background将布局背景设置为示例图像的。

enter image description here

  

是否可以添加更多图像以便这种方式使用?

是的,只需将它们添加到文件夹即可。

  

是否可以添加其他资源?布局?字符串?

它似乎也不支持。如果您尝试定义其他类型的资源,则由于无法识别关键字而导致一堆语法错误,或者您无法使用tools:src="@sample/表示法引用它们。

  

此新功能是否有更多功能?

此时不确定。

答案 1 :(得分:4)

在我这边,我尝试使用图片文件作为使用tools:src="..."的Imageview的占位符:我确实将我的PNG文件放在sampledata子目录中,但它不会显示或显示为破碎的图像。

经过一些小小的尝试后,我找到了解决问题的方法:

  • 创建一个名为sampledata
  • drawable子目录
  • 将您的png / jpg文件放在此目录中,并使用“_drawable”附加文件名。例如:image.png - &gt; image_drawable.png

只有这样,占位符出现了。 您可以根据需要从drawable创建子目录,以保持样本图像的有序性,但重要的是drawable仍然是父目录。

我没有任何文件或消息来支持我,但我认为它对同样情况下的其他人有用。

答案 2 :(得分:1)

我想您正是在我之前回答过的人才能得到其他问题的答案,但是我可以为您提供针对此问题的解决方法

  

是否可以添加更多图像以这种方式使用?

我已经面对这个问题很长时间了,并且找到了解决方法。

我的问题是,如果您不知道,我不能在placeHolderImage中使用SimpleDraweeView,而ImageView是Facebook Fresco库的一部分。但是由于SimpleDraweeView并不像SimpleDraweeView那样复杂,并且由于变通方法适用于ImageView,所以即使我没有尝试使用 { "data": [ { "image": "@drawable/ic_jam_jar" }, { "image": "@drawable/ic_rice_bag" }, { "image": "@drawable/store_avatar" }, { "image": "@drawable/exclamation_icon" } ] } ,它也可能当然也可以使用它。

这是解决方案。

您只需要在sampledata文件夹中创建一个json文件,但是这一次,您必须这样做。

drawable

如您所见,我使用了image资源的引用来代替RecyclerView键的值。就我而言,它是可行的。您在这里得到的是一系列图像资源,您可以在<androidx.recyclerview.widget.RecyclerView android:id="@+id/product_desc_rec_view" android:layout_width="match_parent" android:layout_height="350dp" tools:itemCount="6" app:spanCount="6" android:orientation="vertical" app:layoutManager="androidx.recyclerview.widget.GridLayoutManager" tools:listitem="@layout/product_desc_item" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> 项中使用这些图像资源,以在Android Studio的设计视图中进行预览。

我的RecyclerView代码如下

itemCount

请记住不要跳过我使用的任何其他标签,因为如果您错过任何标签,它可能不会在预览中产生所需的输出。您可以根据对使用的图像数量的要求自定义orientation属性值。 另外,根据您的使用情况,vertical可能是horizontaltools:src="@sample/your_json_file.json/data/image

您只需在recyclerview项目xml文件的ImageView中使用ax即可在其中加载示例图像。

尝试一下,让我知道这是否适合您的情况。

答案 3 :(得分:0)

tools:src =“ @ tools:sample / avatars [5]”

<androidx.appcompat.widget.AppCompatImageView
    android:id="@+id/imageView"
    android:layout_width="50dp"
    android:layout_height="50dp"
    tools:src="@tools:sample/avatars[5]"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"/>
相关问题