[英]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与任何Activity
或AppCompatActivity
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.