向PreferenceScreen片段添加工具栏

4

我正在尝试使用带有工具栏的 Android Jetpack 设置 指南。指南中说根标签应为 <PreferencesScreen>,因此我无法在 xml 中包含工具栏。我使用的是 NoActionBar 主题。根据 Android Jetpack 指南 支持应用栏变体建议从活动中删除顶部栏,而是在每个目标片段中定义它。所以我有以下文件:

preferences.xml

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

    <SwitchPreferenceCompat
        app:key="notifications"
        app:title="Enable message notifications" />

    <Preference
        app:key="feedback"
        app:summary="Report technical issues or suggest new features"
        app:title="Send feedback" />


</PreferenceScreen>

SettingsFragment.kt

class SettingsFragment: PreferenceFragmentCompat(){
    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.preferences, rootKey)
    }
}

一切运行良好,设置片段也能够正常打开,但由于我使用了NoActionBar主题,似乎没有办法在不在main_activity中定义的情况下添加工具栏。我的评估正确吗?还是说我仍然可以在首选项片段中添加自定义工具栏?

尝试使用SettingsActivity,在onCreate中将其替换为片段。这就是我在Java中所做的。不过我对kt不熟悉! - private static
这就是问题所在,我不想创建一个全局工具栏(在活动中)。根据他们的指南,应该可以为每个片段实现一个工具栏。这就是我在这里发布这个问题的原因。感谢您的建议。 - Texts
3个回答

10
也许现在已经晚了,但您可以通过以下方式实现 ->
  • 定义首选主题:
<style name="PrefsTheme" parent="PreferenceThemeOverlay.v14.Material">
    <item name="android:layout">@layout/fragment_settings</item>
</style>
  • fragment_settings.xml:
<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">

<include layout="@layout/item_toolbar" />

<!-- the id is defined by the support lib. -->
<FrameLayout
    android:id="@android:id/list_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:targetApi="n" />

</LinearLayout>
  • 将其设置在您的应用程序主题中:
<style name="AppTheme" parent="Theme.MaterialComponents.Light.NoActionBar">
    <item name="preferenceTheme">@style/PrefsTheme</item>
</style>

然后在您的SettingsFragment中,您可以按ID查找工具栏并对其进行所有自定义:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    view.findViewById<Toolbar>(R.id.toolbar)?.let {
        // Customize toolbar 
    }
} 

3

这个问题的另一个解决方案是,在“首选项”屏幕上,您可以使用“首选项类别”,并使用布局属性。

我所说的代码片段:

<PreferenceCategory
        android:title="Prueba"
        android:textSize="10dp"
        android:layout="@layout/activity_titulo"

        />

activity_titulo代码:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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="wrap_content"
    tools:context=".activities.NuevoParteActivity">

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/id_cabecera"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.GVenaPDA.AppBarOverlay">

        <TextView
            android:id="@+id/title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:minHeight="?actionBarSize"
            android:padding="@dimen/appbar_padding"
            android:text="@string/app_name"
            android:textAppearance="@style/TextAppearance.Widget.AppCompat.Toolbar.Title" />

        <TextView
            android:id="@+id/settings_general"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:gravity="center"

            android:padding="5dp"
            android:text="@string/cabecera_info"
            android:textAppearance="@style/TextAppearance.AppCompat.Widget.ActionBar.Menu"
            />

    </com.google.android.material.appbar.AppBarLayout>

</androidx.coordinatorlayout.widget.CoordinatorLayout>

0
class SettingsFragment : PreferenceFragmentCompat() {
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)
    val root = ((listView.parent as FrameLayout).parent as LinearLayout)
    val toolbar = LayoutInflater.from(requireContext())
        .inflate(R.layout.toolbar, root, false) as MaterialToolbar
    toolbar.title = getText(R.string.title_settings)
    root.addView(toolbar, 0)
}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
    addPreferencesFromResource(R.xml.preferences);
}}

在preferences.xml被填充后,你动态地添加了一个工具栏视图。我尝试了@Yerón Barreiro Martínez的解决方案。然而,我无法在片段页面上访问工具栏实例。因此,我无法设置标题或点击监听器。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接