由于不推荐使用PreferenceFragment,因此我使用PreferenceFragmentCompat.
缩进很可能是因为图标而出现,但我不使用它们(默认是没有图标).
我试图将图标属性设置为android:icon="@null"或android:color/transparent,但它没有帮助.
替换片段的功能:
private fun replaceFragment(fragment: Fragment) {
supportFragmentManager.beginTransaction()
.replace(R.id.fragment_layout, fragment)
.commit()
}
Run Code Online (Sandbox Code Playgroud)
content_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
tools:showIn="@layout/app_bar_main">
<FrameLayout
android:id="@+id/fragment_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Run Code Online (Sandbox Code Playgroud)
我的设置片段:
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) …Run Code Online (Sandbox Code Playgroud)