参考可绘制图像,标题为数字
Reference drawable image titled as a number
如果文件名只是一个数字,你能引用一个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
简答:没有
更长的答案:
每个包含资源的文件名都用于打包 - 它将被指定为 R.java 文件中 public static final int
的字段名称。这意味着除其他限制外,资源名称还必须满足 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;
长话短说,这是 java 中的现有行为,Android 已经建立,保留相同的限制。
此外,它更进一步:R.java 并非特定于可绘制对象。您会注意到在 strings.xml
文件中不能有 1.xml
布局甚至 <string name="1">
。
如果文件名只是一个数字,你能引用一个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
简答:没有
更长的答案:
每个包含资源的文件名都用于打包 - 它将被指定为 R.java 文件中 public static final int
的字段名称。这意味着除其他限制外,资源名称还必须满足 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;
长话短说,这是 java 中的现有行为,Android 已经建立,保留相同的限制。
此外,它更进一步:R.java 并非特定于可绘制对象。您会注意到在 strings.xml
文件中不能有 1.xml
布局甚至 <string name="1">
。