【Android 基础 3.3】Support libraries

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, and minSdkVersion.
  • 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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值