gpt4 book ai didi

android - 引用可绘制图像,标题为数字

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:03:50 27 4
gpt4 key购买 nike

如果文件名只是一个数字,你能引用一个 drawable 吗?

Example: If I have an image name "1.jpg" in res/drawable it will give me an error if I try to reference it using R.drawable.1

If I have an image name "one.jpg" in res/drawable it will work fine if I try to reference it using R.drawable.one

最佳答案

简短回答:否。

更长的答案:

每个包含资源的文件名都用于打包 - 它将被指定为 public static final int 的字段名称在 R.java 文件中。这意味着除其他限制外,资源名称还必须满足 Java 变量名称的条件。

注意什么one of the Java tutorials让我们想起 Java 变量命名:

A variable's name can be any legal identifier — an unlimited-length sequence of Unicode letters and digits, beginning with a letter, the dollar sign "$", or the underscore character "_".

因此以下是有效的 Java 变量名并且可以编译:

public static final int _1=0x7f020000;
public static final int one=0x7f020001;

虽然下面的变量名会导致编译错误:

public static final int 1=0x7f020000;

长话短说,这是 Android 构建的 java 中的现有行为,保留相同的限制。

此外,它更进一步:R.java 并不特定于可绘制对象。你会注意到你不能有 1.xml布局甚至 <string name="1">在你的strings.xml文件。

关于android - 引用可绘制图像,标题为数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28521248/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com