Useful Android system resources

后端 未结 4 704
醉话见心
醉话见心 2020-12-12 15:11

Android comes with lots of system resources (android.R) that can be used to save you time and make your application lighter.

For example, I recently dis

相关标签:
4条回答
  • 2020-12-12 15:29

    You can access system resources from xml by qualifying them with the android package name, i.e. "@android:string/ok"

    0 讨论(0)
  • 2020-12-12 15:29

    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"...

    I'm not sure how good the other languages are, so I would use an own translation.

    0 讨论(0)
  • 2020-12-12 15:39

    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 not available through the android.R class. Most of the time I have to import them into my Eclipse workspace anyway. :-(

    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()).

    0 讨论(0)
  • 2020-12-12 15:42

    You can find a full listing of all system resources in the android package.

    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.

    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.
    0 讨论(0)
提交回复
热议问题