Android 有用的安卓系统资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2631278/
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
Useful Android system resources
提问by hpique
Android comes with lots of system resources (android.R
) that can be used to save you time and make your application lighter.
Android 附带了大量系统资源 ( android.R
),可用于节省您的时间并使您的应用程序更轻巧。
For example, I recently discovered that Android provides localized strings for Yes (android.R.string.yes
), No (android.R.string.no
), Cancel (android.R.string.cancel
) and Ok (android.R.string.ok
), among other strings.
例如,我最近发现 Android 为 Yes ( android.R.string.yes
)、No ( android.R.string.no
)、Cancel ( android.R.string.cancel
) 和 Ok ( android.R.string.ok
) 等字符串提供了本地化的字符串。
What other system resources do you recommend using? Or is there a reason to avoid using system resources?
您建议使用哪些其他系统资源?或者是否有理由避免使用系统资源?
Edit: As noted by Tomas, some of this resources might not produce the results you would expect (particularly, android.R.string.yes/no
returns OK/Cancel
instead of Yes/No
, as reported here). For greater control, you can copy system resources from the Android source code.
编辑:如前所述由Tomas,一些这方面的资源可能不会产生你所期望的结果(特别是android.R.string.yes/no
收益OK/Cancel
而不是Yes/No
如报道这里)。为了更好地控制,您可以从 Android 源代码中复制系统资源。
回答by Dan Lew
You can find a full listing of all system resources in the android package.
您可以在android 包中找到所有系统资源的完整列表。
Every time I want to do something on Android I check to see if there's a system resource that covers what I want to do. It is helpful to import the Android source code (in particular, their /res/ folder) when searching for already-implemented resources that you might want, so you can see their specific implementation.
每次我想在 Android 上做某事时,我都会检查是否有涵盖我想做的事情的系统资源。在搜索您可能想要的已经实现的资源时,导入 Android 源代码(特别是它们的 /res/ 文件夹)很有帮助,这样您就可以看到它们的具体实现。
Personally, I find myself most often using:
就个人而言,我发现自己最常使用:
- Built-in Android layouts for standard tasks, such as spinner dropdowns.
- Android ids (
android.R.id
), because you are often required to use these if you want to use some of Android's widgets (for example,TabHost/TabWidget
requires you to use "android:id/tabhost
", "android:id/tabs
" and "android:id/tabcontent
" if you want to implement an XML layout). - Built-in colors, especially
android.R.color.transparent
. - Android's built-in fade-in and fade-out animations in
android.R.anim
.
- 用于标准任务的内置 Android 布局,例如微调下拉菜单。
- Android ids (
android.R.id
),因为如果要使用某些 Android 小部件,通常需要使用这些ID(例如,如果要实现 XML 布局,则TabHost/TabWidget
需要使用“android:id/tabhost
”、“android:id/tabs
”和“android:id/tabcontent
”)。 - 内置颜色,尤其是
android.R.color.transparent
. - Android 中内置的淡入和淡出动画
android.R.anim
。
回答by ehartwell
You can access system resourcesfrom xml by qualifying them with the android package name, i.e. "@android:string/ok"
您可以通过使用 android 包名称限定它们来从 xml访问系统资源,即"@android:string/ok"
回答by Neil Traft
As CommonsWare mentioned, you don't have to download the Android source repository to inspect the resources; just go to <android-sdk-dir>/platforms/android-X.X/data/res/
. However, if you start using these you will quickly become disappointed to find that most of them are notavailable through the android.R
class. Most of the time I have to import them into my Eclipse workspace anyway. :-(
正如CommonsWare 所提到的,您不必下载Android 源代码库来检查资源;只是去<android-sdk-dir>/platforms/android-X.X/data/res/
。但是,如果您开始使用这些,您很快就会失望地发现它们中的大多数都无法通过android.R
课程获得。大多数情况下,无论如何我都必须将它们导入到我的 Eclipse 工作区中。:-(
My favorite resources are the drawables that show differently based on a control's focused/pressed/disabled states (like android.R.drawable.btn_default). I use those or tweak them to create custom buttons for my apps. As you can see if you look at /data/res/drawable/btn_default.xml
, they are defined as a <selector>
XML element, which gets inflated into a StateListDrawable
at runtime. You could also create your own StateListDrawables and they're super useful, both as View backgrounds and as a button's "compound drawable" (see TextView.setCompoundDrawables()
).
我最喜欢的资源是根据控件的聚焦/按下/禁用状态(如 android.R.drawable.btn_default)显示不同的可绘制对象。我使用这些或调整它们来为我的应用程序创建自定义按钮。正如您在查看 时所看到的/data/res/drawable/btn_default.xml
,它们被定义为一个<selector>
XML 元素,它StateListDrawable
在运行时被膨胀为。您还可以创建自己的 StateListDrawables,它们非常有用,既可以用作视图背景,也可以用作按钮的“复合可绘制对象”(请参阅 参考资料TextView.setCompoundDrawables()
)。
回答by Thomas_D
Please note that the translations are really bad.
请注意,翻译真的很糟糕。
For example the German "no" in android-25 is "Abbrechen" which actually means "cancel". "Yes" is translated as "OK"...
例如,android-25 中的德语“no”是“Abbrechen”,实际上意思是“取消”。“是”翻译为“好”...
I'm not sure how good the other languages are, so I would use an own translation.
我不确定其他语言有多好,所以我会使用自己的翻译。