Android fragments 如何使用导航组件在单个活动设计中使用首选项?

Android fragments 如何使用导航组件在单个活动设计中使用首选项?,android-fragments,android-activity,android-toolbar,android-preferences,android-navigation,Android Fragments,Android Activity,Android Toolbar,Android Preferences,Android Navigation,我想使用导航组件迁移到单活动设计。我正在使用一个活动,其他活动是片段。对于一些屏幕,我只有布局,没有偏好。将这些碎片充气没有问题。但当我试着优先工作时,我遇到了问题 我的要求是: 我需要在一个片段中膨胀工具栏和首选项列表。 我的做法: 使用以下代码添加首选项 SettingFragment.java @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {

我想使用导航组件迁移到单活动设计。我正在使用一个活动,其他活动是片段。对于一些屏幕,我只有布局,没有偏好。将这些碎片充气没有问题。但当我试着优先工作时,我遇到了问题

我的要求是: 我需要在一个片段中膨胀工具栏和首选项列表。 我的做法:

  • 使用以下代码添加首选项
  • SettingFragment.java

     @Override
         public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
          setPreferencesFromResource(R.xml.setting_main, rootKey);
     }
    
  • 为导航组件配置应用程序栏

     @Override
     public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
         super.onViewCreated(view, savedInstanceState);
    
         mNavController = Navigation.findNavController(view);
         AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(mNavController.getGraph()).build();
         Toolbar toolbar = view.findViewById(R.id.the_toolbar);
    
         NavigationUI.setupWithNavController(toolbar, mNavController, appBarConfiguration);
     }
    
  • 在应用程序主题中,我使用了一个包含工具栏和框架布局的布局作为首选项

    <style name="Theme.MyApp" parent="@style/Theme.AppCompat.Light.NoActionBar">
     ...
      <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
    </style>
    
    <style name="PreferenceThemeOverlay">
       <item name="android:layout">@layout/fragment_settings </item>
    </style>
    

  • 仍然陷在这个问题中,急切地等待反馈我陷在这个问题中,急切地等待反馈
      <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
         android:orientation="vertical"
         android:layout_width="match_parent"
         android:layout_height="match_parent">
    
         <androidx.appcompat.widget.Toolbar
            android:id="@+id/the_toolbar"
            android:layout_width="match_parent"
            android:layout_height="?android:attr/actionBarSize"
            android:layout_gravity="bottom"
            android:layout_marginBottom="12dp"/>
    
           <FrameLayout
              android:layout_width="match_parent"
              android:layout_height="wrap_content"
              android:id="@android:id/list_container">
           </FrameLayout>
    
       </LinearLayout>
    
    <androidx.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
       android:key="MyApp"
       android:title="My application">
    
      <androidx.preference.Preference
        android:icon="@drawable/ic_list1"
        android:key="PreferenceList1"
        android:title="PreferenceList1" />
    
      <androidx.preference.PreferenceCategory android:key="SecondCategory">
    
         <androidx.preference.Preference
            android:icon="@drawable/ic_list2"
            android:key="PreferenceList2"
            android:title="PreferenceList2" />
    
        </androidx.preference.PreferenceCategory>
    
    </androidx.preference.PreferenceScreen>
    
     @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable 
      Bundle savedInstanceState) {
      return inflater.inflate(R.layout.setting_main_fragment, container, false);
    }