2018-11-14 13:31:21
What you’ll learn
- How to indicate support library classes in your app.
- How to tell the difference between the values for
compileSdkVersion,targetSdkVersion, andminSdkVersion. - How to recognize deprecated or unavailable APIs in your code.
- More about the Android support libraries.
compileSdkVersion, targetSdkVersion, and minSdkVersion
- compileSdkVersion: The compile version is the Android framework version your app is compiled with in Android Studio.
- minSdkVersion: The minimum version is the oldest Android API version your app runs under.
- targetSdkVersion: The target version indicates the API version your app is designed ans tesed for.If the API of the Android platform is higher than this number (that is, your app is running on a newer device), the platform may enable compatibility behaviors to make sure that your app continues to work the way it was designed to. For example, Android 6.0 (API 23) provides a new runtime permissions model. If your app targets a lower API level, the platform falls back to the older install-time permissions model.
Change color
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
if (savedInstanceState != null) {
helloText.setTextColor(savedInstanceState.getInt("color", R.color.black))
}
changeColorButton.setOnClickListener {
val colorName = colorArray[Random().nextInt(20)]
// get resource indentifier
val resourceName = resources.getIdentifier(colorName, "color", applicationContext.packageName)
// get color int
val colorRes = ContextCompat.getColor(this, resourceName)
helloText.setTextColor(colorRes)
}
}
// save color status
override fun onSaveInstanceState(outState: Bundle) {
super.onSaveInstanceState(outState)
outState.putInt("color", helloText.currentTextColor)
}

Summary
Android uses three directives to indicate how your app should behave for different API versions:
minSdkVersion: the minimum API version your app supports.compileSdkVersion: the API version your app should be compiled with.targetSdkVersion: the API version your app was designed for.
The ContextCompat class provides methods for compatibility with context and resource-related methods for both old and new API levels.
本文详细解析了Android开发中compileSdkVersion、targetSdkVersion和minSdkVersion的区别与作用,以及如何在应用中实现颜色动态变化。通过实例代码展示了如何在运行时根据随机选择的颜色名称,获取其资源标识符并设置文本颜色。
1189

被折叠的 条评论
为什么被折叠?



