简体   繁体   English

是以下错误中的加号吗:android:icon =“ @ + drawable /

[英]Is the plus sign in the following an error: android:icon="@+drawable/

I have been looking at this Android drawing app on github: 我一直在github上查看这个Android绘图应用程序:

https://github.com/valerio-bozzolan/AcrylicPaint https://github.com/valerio-bozzolan/AcrylicPaint

In an XML file defining the layout for a menu, all the android:icon lines have a plus sign before the drawable . 在定义菜单布局的XML文件中,所有android:icon行在drawable之前都有一个加号。 None of the drawable examples I've found on stackoverflow or elsewhere have that plus sign. 我在stackoverflow或其他地方找不到的drawable示例都没有加号。 And Android Studio 2.2.3 is giving me a "Cannot resolve symbol" error for each of them. Android Studio 2.2.3分别给了我一个“无法解析符号”错误。 If I remove the plus sign, that error goes away. 如果删除加号,该错误将消失。 But the app seems to work fine with the plus signs, too. 但是该应用程序似乎也可以使用加号运行。 Are the plus signs just plain wrong? 加号是否完全是错误的? Or is there something going on here that I don't understand? 还是这里发生了我不理解的事情?

The file is /app/res/menu/main.xml, if you want to look at it. 如果要查看该文件,则为/app/res/menu/main.xml。 Here's a screenshot of some sample code, with errors in red text. 这是一些示例代码的屏幕截图,带有红色文本错误。 Thanks for any help you can provide. 感谢您的任何帮助,您可以提供。

在此处输入图片说明

Sloppy Mistake .You must use @drawable instead of @+drawable . 马虎的错误。您必须使用@drawable而不是@ + drawable

android:icon="@drawable/

Shows you to access drawables that you keep inside your app's drawable folder. 显示您访问保存在应用程序的drawable文件夹中的可绘制对象。

So the plus sign looks the mystery in your case. 因此,加号看起来很神秘。 Let me give a detailed explanation of the plus sign. 让我详细说明加号。

  1. When we give an id to any view, it is in general new and that id reference does not exist in R.id class. 当我们给任何视图一个id时,它通常是新的,并且R.id类中不存在该id引用。
    So when you use something like findViewById(R.id.view) , you can not access that particular id since it is not available. 因此,当您使用诸如findViewById(R.id.view)类的东西时,您将无法访问该特定ID,因为它不可用。
    So to notify the complier that a new reference has to be added in R.id file we declare id as android:@+id/view , here + means add this id to R.id reference. 因此,为了通知编译器必须在R.id文件中添加新的引用,我们将id声明为android:@+id/view ,这里+表示将此ID添加到R.id参考中。

  2. But in case of drawables, styles, colors, etc., you first create them in their respective folders which is accessible by the compiler (say Gradle) and is automatically reference in their respective class like R.drawable, R.style, etc. 但是对于可绘制对象,样式,颜色等,首先要在它们各自的文件夹中创建它们,编译器可以访问它们(例如Gradle),并在它们各自的类(例如R.drawable,R.style等)中自动引用它们。
    So when we refer them in xml, we don't use + sign as that reference is already created during their respective declarations. 因此,当我们在xml中引用它们时,我们不会使用+符号,因为在它们各自的声明期间已经创建了引用。

Only id is coded as android:id="@+id/dummy" 只有id编码为android:id="@+id/dummy"

For drawable and other things you need not have + in it 对于可绘制内容和其他内容,您无需在其中添加+

It can be used like android:icon="@drawable/yourImage" 可以像android:icon="@drawable/yourImage"

android:style="@style/myStyle"

Unlike android:id="@+id/aSampleId" , you cannot generate images on the fly. android:id="@+id/aSampleId" ,您无法即时生成图像。 If you want to access the drawable folder then you need to have an image in the folder itself. 如果要访问可绘制文件夹,则需要在文件夹本身中包含一个图像。

Suppose you have an image in the drawable folder drawable/anImage.png , then you would need to call on it with android:icon="@drawable/anImage" 假设您在drawable文件夹 drawable/anImage.png有一个图像,那么您需要使用android:icon="@drawable/anImage"对其进行调用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM