如何解决如何使用导航组件在单个活动设计中使用偏好?
我想迁移到使用导航组件的单一活动设计。我正在使用一项活动,其他活动是片段。对于某些屏幕,我只有布局,没有偏好。膨胀碎片没有问题。但是当我尝试优先工作时遇到了问题。
我的要求是: 我需要在片段中扩充工具栏和首选项列表。 我的方法:
- 使用以下代码添加首选项。
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>
fragment_settings.xml
<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>
setting_main.xml
<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>
我的问题: 如果我这样做,我可以成功添加工具栏和首选项。但是在从一个片段到另一个片段的交易过程中,我看到标题中出现了一个闪烁的问题。
我搜索了很多,但没有找到任何合适的解决方案。我是新手。
我的问题:
-
我该如何解决这个问题?
-
有什么办法可以在导航组件中使用首选项,并且在 Fragment 中也有工具栏和首选项?
-
对于其他没有偏好的片段,我可以使用此代码进行充气,该代码工作正常,没有闪烁问题。有什么方法可以根据我的要求进行调整吗?
@Override public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) { return inflater.inflate(R.layout.setting_main_fragment,container,false); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。