【问题标题】:Where are the extension function for Android Context?Android Context的扩展功能在哪里?
【发布时间】:2019-11-18 09:57:49
【问题描述】:

作为安卓开发者,我们使用ContextCompat.getColor(context, colorRes)ContextCompat.getDrawable(context, drawableRes)

为了简单起见,我们在Context 上创建了扩展函数,如下所示:

inline fun Context.getCompatColor(@ColorRes colorRes: Int): Color = ContextCompat.getColor(this, colorRes)

这非常有用,所以我想知道它当前是否在 Android KTX 库中。我一直在寻找它,但我找不到它。它在那里吗?如果不是,为什么不在那里?

【问题讨论】:

  • 只是一个小修复@ColorInt inline fun Context.getCompatColor(@ColorRes colorRes: Int): Int = ContextCompat.getColor(this, colorRes)

标签: android android-ktx


【解决方案1】:

以下是 core-ktx 中android.content.Context 的所有扩展函数:

systemService():按类将句柄返回给系统级服务。

withStyledAttributes():在 TypedArray 接收器上执行块。

https://android.github.io/android-ktx/core-ktx/androidx.content/android.content.-context/index.html

【讨论】:

    【解决方案2】:

    试试Context.resources.getColor()它会解决你的问题

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-01-28
      • 2014-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-11-11
      • 1970-01-01
      相关资源
      最近更新 更多