简体   繁体   English

偏好活动与PreferenceFragment?

[英]Preference Activity Vs PreferenceFragment?

In preference Activity we can use these two methods SetContentView(R.layout.main) and addXmlFromResources(R.xml.Preferences) for customizing the preference screen. 在首选Activity中,我们可以使用这两个方法SetContentView(R.layout.main)addXmlFromResources(R.xml.Preferences)来自定义首选项屏幕。 For example see this Adding a button on Prefernce Screen 例如,请在Prefernce Screen上看到这个添加按钮

Is this possible in PreferenceFragment? 这在PreferenceFragment中是否可行?

In PreferenceFragment, I have added addPreferencesFromResource(R.xml.PreferenceScreen); 在PreferenceFragment中,我添加了addPreferencesFromResource(R.xml.PreferenceScreen); in onCreate method. 在onCreate方法中。 When I use onCreateView it's getting force closes. 当我使用onCreateView时,它会被强制关闭。 I have tried Layout Inflator also. 我也试过布局充气机。 It's not working. 它不起作用。

So is this possible only on preference Activity? 那么这只能在首选项Activity上实现吗? not Preference Fragment? 不是偏好片段?

PS- I am using support V 13 Library. PS-我正在使用支持V 13库。 So I have created Preference Fragment in Fragment Pager Adapter. 所以我在Fragment Pager Adapter中创建了Preference Fragment。 Please don't suggest to me to create Preference Activity for preference Fragment 请不要建议我为首选片段创建首选项活动

You can use 您可以使用

addPreferencesFromResource(int res);

in both PreferenceFragment and PreferenceActivity , inside the onCreate() method. 这两个 PreferenceFragmentPreferenceActivity ,内侧onCreate()方法。 Bear in mind that PreferenceFragment should be used in post Honeycomb Android Versions as a replacement for PreferenceActivity . 请记住, PreferenceFragment应该在后Honeycomb Android版本中用作PreferenceActivity的替代品。

If you want a custom-layout for your PreferenceActivity , you can call setContentView() in the onBuildHeaders() method, but not in the onCreate() . 如果需要PreferenceActivity的自定义布局,可以在onBuildHeaders()方法中调用setContentView() ,但不能onCreate()调用。

  • If your app supports versions of Android older than 3.0 (API level 10 and lower), you must build the activity as an extension of the PreferenceActivity class. 如果您的应用支持早于3.0的版本(API级别10及更低版本),则必须将该活动构建为PreferenceActivity类的扩展。
  • On Android 3.0 and later, you should instead use a traditional Activity that hosts a PreferenceFragment that displays your app settings. 在Android 3.0及更高版本中,您应该使用传统的Activity来托管显示应用程序设置的PreferenceFragment However, you can also use PreferenceActivity to create a two-pane layout for large screens when you have multiple groups of settings. 但是,如果有多组设置,还可以使用PreferenceActivity为大屏幕创建双窗格布局。

see this link 看到这个链接

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM