Android 将首选项屏幕放在片段内

Android 将首选项屏幕放在片段内,android,fragment,preferencescreen,Android,Fragment,Preferencescreen,我想知道是否有可能将我的PreferenceScreen xml放入ConstraintLayout片段中。我希望我的首选项屏幕位于“设置”文本下方 以下是我的片段xml代码: <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.androi

我想知道是否有可能将我的PreferenceScreen xml放入ConstraintLayout片段中。我希望我的首选项屏幕位于“设置”文本下方

以下是我的片段xml代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
    android:background="@drawable/shape_fragment_background"
    tools:context=".ui.fragments.SettingsFragment">

    <TextView
        android:id="@+id/tv_header"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:fontFamily="@font/montserrat"
        android:gravity="center"
        android:text="@string/settings"
        android:textSize="24sp"
        android:textStyle="bold"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

这是我的首选屏幕代码:

<androidx.preference.PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory app:title="@string/settings">

        <Preference
            app:title="@string/language"
            app:summary="@string/language_summary"
            app:icon="@drawable/ic_baseline_language_24">
            <intent android:action="android.settings.LOCALE_SETTINGS" />
        </Preference>

        <SwitchPreferenceCompat
            app:title="@string/reminder"
            app:summaryOn="@string/reminder_summary_on"
            app:summaryOff="@string/reminder_summary_off"
            app:icon="@drawable/ic_baseline_notifications_24"
            app:key="@string/reminder"/>

    </PreferenceCategory>

    <PreferenceCategory android:title="@string/info">

        <Preference
            app:title="@string/about"
            app:icon="@drawable/ic_baseline_info_24" />

    </PreferenceCategory>

</androidx.preference.PreferenceScreen>


感谢您的帮助。

制作一个包含首选项xml的容器片段

class FragmentContainerSettings : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    requireActivity().supportFragmentManager
        .beginTransaction()
        .replace(
            R.id.settings,
            SettingsFragment()
        )
        .commit()
}

override fun onCreateView(
    inflater: LayoutInflater, container: ViewGroup?,
    savedInstanceState: Bundle?
): View? {
    // Inflate the layout for this fragment
    val view = inflater.inflate(R.layout.fragment_container_settings, container, false)

    return view
    }


}
有关首选项屏幕设置xml

class SettingsFragment : PreferenceFragmentCompat() {

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
    setPreferencesFromResource(R.xml.root_preferences, rootKey)

    }

}
FragmentContainerSettings的布局xml。我在这里使用了LinearLayout,但如果需要,可以将其替换为ConstraintLayout。基本上,当您使用navController.navigate(R.id.FragmentContainerSettings)导航到FragmentContainerSettings时,它将创建文本视图并用PreferenceScreen xml替换FrameLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".ui.settings.FragmentContainerSettings"
    >

    <TextView
    android:id="@+id/tv_header"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:fontFamily="@font/montserrat"
    android:gravity="center"
    android:text="@string/settings"
    android:textSize="24sp"
    android:textStyle="bold"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

    <FrameLayout
    android:id="@+id/settings"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    />
</LinearLayout>