简体   繁体   中英

How to change textview font size in recyclerView in Kotlin?

How to change textView font size in RecyclerView (from shared preferences)?

I tried to save size in 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
    }
}

What is the code and where put it in Adapter or Activity?

ok I am guessing that you already have some mechanism to store the font size in shared pref. Now, you just need to ask sharedPref from recycler view adapter. To access sharedPref you can use itemView.getContext() (in java) or itemView.context (in kotlin). Now you have context(lets say you named it ctx ), you can do this.

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

Hope this works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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