简体   繁体   中英

How to add On-screen button to PreferenceScreen

I' m using AndroidX Preference library version 1.1.1. It provides components like SwitchPreferenceCompat and MultiSelectListPreference .

And in Android Settings Design Guidelines , there are samples of On-screen button component, when the Preference API does not provide any sub components of button. 设置页面设计指南中的屏幕按钮

I know there are more than 10 questions about how to add button here in stackoverflow, but I just don' t understand why we have to add custom layout by ourselves. Isn' t it(I mean, buttons in Setting Screen) an officially recommended standard practice? LOGOUT or CLEAR DATA buttons are common at the end of app' s setting page. I am confused.

Maybe I know why now...

The number, arrangement, and style of buttons at footer are depend on the needs, so a custom preference is necessary. In fact, we have to do the same when we want to change the layout or style of a given standard preference.

In androidX Preference instead of old Preference, it is easier than before:

class FooterPreference(context: Context, attrs: AttributeSet): Preference(context, attrs) {

    override fun onBindViewHolder(holder: PreferenceViewHolder?) {
        super.onBindViewHolder(holder)
        holder?.itemView?.findViewById<Button>(R.id.btn)?.setOnClickListener {

        }
    }
}

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