繁体   English   中英

如何在 Kotlin 中的 recyclerView 中更改 textview 字体大小?

[英]How to change textview font size in recyclerView in Kotlin?

如何在 RecyclerView 中更改 textView 字体大小(从共享首选项)?

我试图在 SaveData class 中保存大小:

class SaveData(context: Context) {
    private val sharedPreferences: SharedPreferences =
        context.getSharedPreferences("file", Context.MODE_PRIVATE)

fun setFontSize(size: Float?) {
        val editor = sharedPreferences.edit()
        editor.putFloat("FontSize", size!!)
        editor.apply()
    }

    fun loadFontSize(): Float {
        val size = sharedPreferences.getFloat("FontSize", 0f)
        return size
    }
}

代码是什么,放在 Adapter 或 Activity 中的什么位置?

好的,我猜你已经有了一些机制来将字体大小存储在共享首选项中。 现在,您只需要从回收器视图适配器中询问 sharedPref。 要访问 sharedPref,您可以使用 itemView.getContext itemView.getContext() (在 java 中)或itemView.context (在 kotlin 中)。 现在您有了上下文(假设您将其命名为ctx ),您可以执行此操作。

val pref= SaveData(ctx)
val fontSize= pref.loadFontSize()

希望这有效。

暂无
暂无

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

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