将首选项屏幕放在片段内
Place Preference Screen Inside Fragment
我想知道是否可以将我的 PreferenceScreen xml 放在 ConstraintLayout 片段中。我希望我的 PreferenceScreen 位于“设置”文本下方
这是我的片段 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>
这是我的 PreferenceScreen 代码:
<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
}
}
对于 PrefereneceScreen 设置 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 时,它将创建文本视图并将 FrameLayout 替换为 PreferenceScreen xml
<?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>
我想知道是否可以将我的 PreferenceScreen xml 放在 ConstraintLayout 片段中。我希望我的 PreferenceScreen 位于“设置”文本下方
这是我的片段 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>
这是我的 PreferenceScreen 代码:
<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
}
}
对于 PrefereneceScreen 设置 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 时,它将创建文本视图并将 FrameLayout 替换为 PreferenceScreen xml
<?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>