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

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

问题描述

背景

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

举个例子,你可以使用这个:

并在预览中获取此内容:

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

这是我从

然后你可以像这样引用它们,使用 @sample/

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

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

这个结构

结合这些参考

<图像视图android:layout_width="match_parent"android:layout_height="100dp"工具:src="@sample/avatar"/><图像视图android:layout_width="match_parent"android:layout_height="100dp"工具:src="@sample/jpeg"/><图像视图android:layout_width="match_parent"android:layout_height="100dp"工具:src="@sample/vector"/></LinearLayout>

生成此预览.请注意我如何利用 tools:background 将布局背景设置为示例图像.

<块引用>

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

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

<块引用>

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

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

<块引用>

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

目前不确定.

Background

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" ... />

And get this in the preview:

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:

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

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.

What I tried

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.

The questions

  1. What's even the name of this feature?
  2. How can I put the image file into the project, and use it as a placeholder this way ? In which folder?
  3. Is it possible to add more images to be used this way?
  4. Is it possible to add other resources? Layouts? Strings?
  5. Are there more capabilities of this new feature?

解决方案

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?

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.

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

You can then reference them like this, with @sample/

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

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:

A structure of this

Combined with these references

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

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?

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?

Unsure at this time.

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

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