简体   繁体   English

设置活动/片段中的工具栏

[英]Toolbar in a settings activity/fragment

I have searched the web for solution to my problem and can't seem to find anything that will work.我在网上搜索了我的问题的解决方案,但似乎找不到任何可行的方法。 I have implemented the following into my app and it does work.我已经在我的应用程序中实现了以下内容并且它确实有效。 However, I would like to to add a toolbar at the top with the left arrow to allow the user to go back.但是,我想在顶部添加一个带有左箭头的工具栏,以允许用户返回。 can anyone help me out with this.谁能帮我解决这个问题。

public class ActivitySettings extends PreferenceActivity {

     @Override
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          getFragmentManager().beginTransaction().replace(android.R.id.content, new MyPreferenceFragment()).commit();
     }

     public static class MyPreferenceFragment extends PreferenceFragment {

          @Override
          public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          addPreferencesFromResource(R.xml.preferences);
          }
     }
}

Thanks for any and all help.感谢您的任何帮助。

This is the exact use case of the Preferences Support Library - it allows you to not use PreferenceActivity and instead use a PreferenceFragmentCompat .这是Preferences Support Library的确切用例 - 它允许您使用PreferenceActivity而是使用PreferenceFragmentCompat This allows you to use AppCompatActivity and the Toolbar support built into it.这允许您使用AppCompatActivity和内置于其中的Toolbar支持。

The PreferenceActivity doesn't extend the AppCompatActivity . PreferenceActivity不扩展AppCompatActivity

As solution you can use the PreferenceFragmentCompat with any Activity or AppCompatActivity作为解决方案,您可以将PreferenceFragmentCompat与任何ActivityAppCompatActivity

In this case you have to set preferenceTheme in your theme:在这种情况下,您必须在主题中设置preferenceTheme主题:

<style name="AppTheme" parent="@style/Theme.AppCompat.Light">
  ...
  <item name="preferenceTheme">@style/PreferenceThemeOverlay</item>
</style>

In this way you can customize the preferenceTheme to style the layouts used for each preference.通过这种方式,您可以自定义preferenceTheme以设置用于每个首选项的布局的样式。

Moreover with the new 22.1+ appcompat you can use the AppCompatDelegate to extend AppCompat's support to any Activity.此外,使用新的 22.1+ appcompat,您可以使用AppCompatDelegate将 AppCompat 的支持扩展到任何活动。

You can check this official link to AppCompatPreferenceActivity , where you can find an example of this technique.您可以查看此AppCompatPreferenceActivity 的官方链接,您可以在其中找到此技术的示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在片段活动中添加工具栏 - Add toolbar in fragment activity 从片段设置活动工具栏行为 - Set Activity Toolbar Behavior from Fragment 为什么工具栏正在覆盖另一个活动的片段? - Why Toolbar is Overriding another activity's fragment? 覆盖片段内的活动工具栏(并在片段被销毁时恢复) - Override the activity toolbar inside a fragment (and restore when the fragment is destroyed) Android - 在处理带片段的导航时更新活动中的工具栏标题 - Android - update toolbar title in activity while handle back navigation with fragment 如何使用活动和片段在自定义工具栏中设置菜单项? - How to set Menu Items in Custom Toolbar with a activity and a fragment? 在其 ViewPager 中使用带有 ScrollView 的片段隐藏选项卡式活动中的工具栏 - Hiding Toolbar in tabbed activity with fragment with ScrollView in its ViewPager 片段中存在TabLayout时如何隐藏活动工具栏? - How to hide toolbar of activity when tablayout is present in fragment? Android Studio - 设置活动只有一个片段/没有标题 - Android Studio - Settings Activity with just one fragment/no Headers 从“设置”片段返回后,杀死活动中的媒体播放器 - Kill mediaplayer in an Activity after returning from Settings Fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM