如何将新的占位符资源放入Android Studio项目("tools:sample"资源)? [英] How to put new placeholder resources into Android Studio project ("tools:sample" resources)?

查看:214
本文介绍了如何将新的占位符资源放入Android Studio项目("tools:sample"资源)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有时候,您希望在布局文件中放置一些仅在IDE上显示的占位符.

Sometimes, you wish to put some placeholders to be shown only on the IDE, in layout files.

作为示例,您可以使用以下代码:

As an example, you can use this:

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

并在预览中获取它:

此类文件不属于您在构建应用时获得的APK的一部分,因此可以安全地用于开发.

Such files are not part of the APK you get when you build your app, so it's safe to use just for developing.

这是我从 此处 得知的内容:

This is what I was told from here:

使用3.0中的示例数据,您现在可以使用占位符图像 不属于已编译apk的一部分.您只需要在其中有一个sampledata目录 您的项目带有一个子目录,其中包含您想要的所有图像 用作占位符.您可以从工具"中引用这些图像 属性.此外,还有一些预定义的库存图片,例如 @ sample/头像或@ sample/background/scenic

With sample data in 3.0, you can now have placeholder images that are not part of the compiled apk. You just need a sampledata directory in your project with a subdirectory containing all the images you want to use as placeholders. You can refer those images from "tools" attributes. Also, there are predefined stock images like @sample/avatars or @sample/background/scenic

问题

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

The problem

I can't find how to add more of such images into the project (so that they will only be used in the IDE, and not a part of the APK), and if there is a way to put other resources, other than images.

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

In fact I can't find the docs of this feature.

我尝试将图像放在"res/sampledata"上,并尝试在"res/sample"上,但是在两种情况下都无法达到.

I tried to put an image on "res/sampledata" and tried on "res/sample", but I wasn't able to reach it in both cases.

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

推荐答案

此功能的名称甚至是什么?

What's even the name of this feature?

未经官方消息来源证实,但可能称为样本数据".

Unconfirmed by an official source, but it's likely called "Sample Data".

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

How can I put the image file into the project, and use it as a placeholder this way ? In which folder?

与图像,字体等资源不同.示例数据不在/res/中(它们不是使用应用程序编译的,因此.将它们放在完全独立的目录中可能会更容易过滤掉) .它们进入/app/sampledata/,例如:/app/sampledata/image.png.

Unlike resources like images, fonts, etc. The sample data does not go in /res/ (they are not compiled with the app, hence. It is probably easier to filter them out by putting them in a totally separate directory). They go in /app/sampledata/, for example: /app/sampledata/image.png.

您可以通过右键单击app来创建sampledata文件夹,然后执行New > Sample Data directory:

You can create the sampledata folder by right clicking on app and do New > Sample Data directory:

然后您可以使用@sample/

<ImageView
    android:layout_width="match_parent"
    android:layout_height="200dp"
    tools:src="@sample/test.png" />

虽然这不会产生任何错误,但令人遗憾的是,该功能现在似乎已出现问题,因为无论是否将图像放置在子目录中,图像都不会显示在预览中(尝试png,jpeg,jpg,xml)

While this does not give any errors, sadly the feature seems bugged right now, since the images do not show up in the preview, regardless of them being placed in a subdirectory or not (tried png, jpeg, jpg, xml).

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

Interestingly, placing a single image in a subdirectory and referring to that subdirectory instead of the specific image seems to work:

此结构

结合这些参考文献

<?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将布局背景设置为示例图像的.

Produces this preview. Notice how I utilized tools:background to set the layout background to a sample image.

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

Is it possible to add more images to be used this way?

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

Yeah, just add them to the folder.

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

Is it possible to add other resources? Layouts? Strings?

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

It does not appear to support either. If you try to define some other type of resource, you either get a bunch of syntax errors due to keywords not being recognized, or you cannot refer to them with the tools:src="@sample/ notation.

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

Are there more capabilities of this new feature?

目前不确定.

这篇关于如何将新的占位符资源放入Android Studio项目("tools:sample"资源)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆