Android PreferenceFragmentCompat标题空白

时间:2018-11-17 11:47:33

标签: android

我不知道为什么使用此Fragment可能会导致问题,看来isIconSpaceReserved = false对于非preferenceCagory很有用

class TFragment : PreferenceFragmentCompat() {


override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
    val context = preferenceManager.context

    val screen = preferenceManager.createPreferenceScreen(context)

    val category1 = PreferenceCategory(context)
    category1.title = "button"
    category1.summary = "what is the problem"
    category1.key = "bbb"

    val swithButton = SwitchPreferenceCompat(context)
    swithButton.title = "button"
    swithButton.summary = "summary"
    swithButton.key = "sss"
    swithButton.isIconSpaceReserved = false


    val swithButton2 = EditTextPreference(context)
    swithButton2.title = "edit"

    val swithButton3 = SeekBarPreference(context)
    swithButton3.title = "seekbar"
    swithButton3.value = 1


    screen.addPreference(category1)
    category1.addPreference(swithButton)

    val category2 = PreferenceCategory(context)
    category2.title = "category2"

    screen.addPreference(category2)

    category2.addPreference(swithButton2)
    category2.addPreference(swithButton3)
    preferenceScreen = screen
}

样式是正常的,我尝试了很多方法,但是没用。

enter image description here

1 个答案:

答案 0 :(得分:0)

现在,我尝试了'androidx.preference:preference:1.1.0-alpha01',很幸运,它修复了PreferenceCategory不支持isIconSpaceReserved的错误。

override fun setPreferenceScreen(preferenceScreen: PreferenceScreen?) {
    super.setPreferenceScreen(preferenceScreen)
    if (preferenceScreen != null) {
        val count = preferenceScreen.preferenceCount
        for (i in 0 until count) {
            if (preferenceScreen.getPreference(i) is PreferenceCategory) {
                val category = preferenceScreen.getPreference(i) as PreferenceCategory
                val childCount = category.preferenceCount
                for (j in 0 until childCount) {
                    category.getPreference(j).isIconSpaceReserved = false
                }
            }
            preferenceScreen.getPreference(i).isIconSpaceReserved = false
        }
    }
}