简体   繁体   English

有用的Android系统资源

[英]Useful Android system resources

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 )提供本地化字符串。 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 ). 编辑:作为由Tomas指出,一些这方面的资源可能不会产生你所期望的结果(特别是android.R.string.yes/no收益OK/Cancel ,而不是Yes/No ,据报道在这里 )。 For greater control, you can copy system resources from the Android source code. 为了获得更好的控制,您可以从Android源代码中复制系统资源。

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. 每次我想在Android上做一些事情我都会检查是否有一个涵盖我想要做的事情的系统资源。 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源代码(特别是其/ res /文件夹 )很有帮助,因此您可以看到它们的具体实现。

Personally, I find myself most often using: 就个人而言,我发现自己经常使用:

  • Built-in Android layouts for standard tasks, such as spinner dropdowns. 用于标准任务的内置Android布局,例如微调器下拉列表。
  • 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). Android ids( android.R.id ),因为你经常需要使用这些,如果你想使用一些Android的小部件(例如, TabHost/TabWidget要求你使用“ android:id/tabhost ”,“ android:id/tabs “和” android:id/tabcontent “如果你想实现XML布局)。
  • Built-in colors, especially android.R.color.transparent . 内置颜色,尤其是android.R.color.transparent
  • Android's built-in fade-in and fade-out animations in android.R.anim . Android中内置的淡入和淡出的动画android.R.anim

您可以通过使用android包名称限定来自xml的系统资源 ,即"@android:string/ok"

As CommonsWare mentioned, you don't have to download the Android source repository to inspect the resources; 正如CommonsWare所提到的,您不必下载Android源代码库来检查资源; just go to <android-sdk-dir>/platforms/android-XX/data/res/ . 只需转到<android-sdk-dir>/platforms/android-XX/data/res/ However, if you start using these you will quickly become disappointed to find that most of them are not available through the android.R class. 但是,如果你开始使用它们,很快就会发现它们中的大部分都不能通过 android.R类获得。 Most of the time I have to import them into my Eclipse workspace anyway. 大多数时候我必须将它们导入我的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). 我最喜欢的资源是基于控件的聚焦/按下/禁用状态(如android.R.drawable.btn_default)显示不同的drawable。 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. 正如您所看到的,如果您查看/data/res/drawable/btn_default.xml ,它们被定义为<selector> XML元素,它在运行时被扩展为StateListDrawable 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() ). 您还可以创建自己的StateListDrawables,它们非常有用,包括View背景和按钮的“复合drawable”(请参阅TextView.setCompoundDrawables() )。

Please note that the translations are really bad. 请注意,翻译非常糟糕。

For example the German "no" in android-25 is "Abbrechen" which actually means "cancel". 例如,android-25中的德语“no”是“Abbrechen”,实际上意味着“取消”。 "Yes" is translated as "OK"... “是”被翻译为“OK”......

I'm not sure how good the other languages are, so I would use an own translation. 我不确定其他语言有多好,所以我会使用自己的翻译。

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

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