Android res文件夹中的可绘制文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2533410/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawable folders in res folder?
提问by Mohit Deshpande
What is the difference between the three drawable folders in the res folder in the project hierarchy? If I have an image to put into a folder, which folder do I put it in?
项目层次结构中res文件夹中的三个drawable文件夹有什么区别?如果我有一张图片要放入文件夹,我应该将它放入哪个文件夹?
回答by CommonsWare
I am going to take a guess that "the three drawable folders" are drawable-ldpi
, drawable-mdpi
, and drawable-hdpi
. In that case, if you stick with all of those folders, you need to put one image in each, sized to match the indicated screen density. This is discussed in the online documentationas well as this blog post. You can find a set of sample projects showing use of different drawable resources based on screen density here.
我将采取猜测,“三个可绘制文件夹”是drawable-ldpi
,drawable-mdpi
和drawable-hdpi
。在这种情况下,如果您坚持使用所有这些文件夹,则需要在每个文件夹中放置一张图像,其大小与指示的屏幕密度相匹配。在线文档和这篇博文中对此进行了讨论。您可以在此处找到一组示例项目,这些项目展示了根据屏幕密度使用不同可绘制资源的情况。
If you are just starting out in Android development, you can get rid of all three of those directories and create a single drawable
directory, putting your image in there. Eventually, though, for a quality application, you will want to test your images on different devices/emulators with different screen densities, and possibly have different images for each density to improve the look of your app.
如果您刚刚开始 Android 开发,您可以删除所有这三个目录并创建一个drawable
目录,将您的图像放在那里。但是,最终,对于高质量的应用程序,您将需要在具有不同屏幕密度的不同设备/模拟器上测试您的图像,并且可能为每个密度使用不同的图像以改善您的应用程序的外观。
回答by Graeme
Here is a reference to multi device options.
这里是多设备选项的参考。
As said by @CommonsWare, you don't needto put resources in anything but res/ layout/ or drawable/ but if you want your program to have a better experience on multiple devices with different screens / density / languages you may want to consider that you have that option.
正如@CommonsWare 所说,除了 res/layout/ 或 drawable/之外,您不需要将资源放在任何东西中,但是如果您希望您的程序在具有不同屏幕/密度/语言的多个设备上有更好的体验,您可能需要考虑你有那个选择。
回答by G_V
Also interesting, though not specific to images, is how android handles resources. It gives them a load order, where more specific means it'll get picked over less specific.
同样有趣的,虽然不是特定于图像,是 android 如何处理资源。它为他们提供了一个加载顺序,其中更具体的意味着它将被挑选而不是具体的。
For example, you have three String values in strings.xml in your values folder. You also have one specific string in a folder called values-en. When android opens your app and your locale matches en, it will load two defaults from the values folder and the third, more specific string, from values-en. If your locale is ru, it will just use the default instead because you have no values-ru folder.
例如,您的 values 文件夹中的 strings.xml 中有三个 String 值。在名为 values-en 的文件夹中还有一个特定的字符串。当 android 打开您的应用程序并且您的语言环境与 en 匹配时,它将从 values 文件夹加载两个默认值,并从 values-en 加载第三个更具体的字符串。如果您的语言环境是 ru,它将只使用默认值,因为您没有 values-ru 文件夹。
The same is true for images. If android can't find images for your specific screen variant, it will use defaults from the drawable folder.
图像也是如此。如果 android 无法为您的特定屏幕变体找到图像,它将使用 drawable 文件夹中的默认值。
In my opinion it's good practice to have default values/images in your generic folders like values/drawable and specific values/images in specific folders. This way both current and future devices will at least have a way to display your content until you can provide specific versions.
在我看来,在通用文件夹中使用默认值/图像是一种很好的做法,例如特定文件夹中的值/可绘制和特定值/图像。通过这种方式,当前和未来的设备至少都可以显示您的内容,直到您可以提供特定版本。