作者:流血的云_-86097 | 来源:互联网 | 2022-12-06 19:23
由于不推荐使用PreferenceFragment,因此我使用PreferenceFragmentCompat.
更换片段后,我从内容中得到左缩进:
缩进很可能是因为图标而出现,但我不使用它们(默认是没有图标).
我试图将图标属性设置为android:icon="@null"
或android:color/transparent
,但它没有帮助.
替换片段的功能:
private fun replaceFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_layout, fragment)
.commit()
}
content_main.xml
我的设置片段:
class SettingsFragment : PreferenceFragmentCompat() {
companion object {
/**
* A preference value change listener that updates the preference's summary
* to reflect its new value.
*/
private val sBindPreferenceSummaryToValueListener = Preference.OnPreferenceChangeListener { preference, value ->
val stringValue = value.toString()
if (preference is ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
val index = preference.findIndexOfValue(stringValue)
// Set the summary to reflect the new value.
preference.setSummary(
if (index >= 0)
preference.entries[index]
else
null)
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.summary = stringValue
}
true
}
}
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.preferences, rootKey)
/**
* Bind preference summary to value for lists and sorting list preferences
*/
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_key_name)))
bindPreferenceSummaryToValue(findPreference(getString(R.string.pref_key_language)))
}
private fun bindPreferenceSummaryToValue(preference: Preference) {
// Set the listener to watch for value changes.
preference.OnPreferenceChangeListener= sBindPreferenceSummaryToValueListener
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference,
PreferenceManager
.getDefaultSharedPreferences(preference.context)
.getString(preference.key, ""))
}
的preferences.xml
styles.xml
版本SDK
ext.support_version = '28.0.0-rc01'
Henrique..
11
如果您将代码迁移到androidx,则可以通过向每个首选项添加以下内容来轻松删除多余的填充:
app:icOnSpaceReserved="false"
您还需要将以下名称空间添加到xml文件的顶部:
xmlns:app="http://schemas.android.com/apk/res-auto"
1> Henrique..:
如果您将代码迁移到androidx,则可以通过向每个首选项添加以下内容来轻松删除多余的填充:
app:icOnSpaceReserved="false"
您还需要将以下名称空间添加到xml文件的顶部:
xmlns:app="http://schemas.android.com/apk/res-auto"
那PreferenceCategory呢