我已经实现了我的偏好设置,如官方指南中所示。
我有一个PreferenceActivity,它会像这样创建PreferenceFragment:
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Bundle extras = getIntent().getExtras();
if (extras != null)
{
Bundle bundle = new Bundle();
_widgetID = extras.getInt(GlobalSettings.EXTRA_WIDGET_ID);
bundle.putInt(GlobalSettings.EXTRA_WIDGET_ID, _widgetID);
WidgetSettingsFragment fragment = new WidgetSettingsFragment();
fragment.setArguments(bundle);
getFragmentManager().beginTransaction().replace(android.R.id.content,
fragment).commit();
}
}
PreferenceFragment从资源中加载首选项,并且它们包含如下所示的首选项子屏幕:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<!-- opens a subscreen of settings -->
<PreferenceScreen
android:key="button_voicemail_category_key"
android:title="@string/voicemail"
android:persistent="false">
<ListPreference
android:key="button_voicemail_provider_key"
android:title="@string/voicemail_provider" ... />
<!-- opens another nested subscreen -->
<PreferenceScreen
android:key="button_voicemail_setting_key"
android:title="@string/voicemail_settings"
android:persistent="false">
...
</PreferenceScreen>
<RingtonePreference
android:key="button_voicemail_ringtone_key"
android:title="@string/voicemail_ringtone_title"
android:ringtoneType="notification" ... />
...
</PreferenceScreen>
...
</PreferenceScreen>
到目前为止,此方法运行良好,但现在我希望在显示首选项子屏幕时在actionBar中添加一个
up-Button
。任何想法如何做到这一点?我试图在 Activity 中设置
setDisplayHomeAsUpEnabled(true)
,但是向上按钮仅显示在主要首选项中(不应显示),而不显示在子屏幕中。我想知道,即使在正式文档中,子屏幕也显示为没有 Activity 的向上按钮:
链接到文档:Settings
任何帮助都非常欢迎
最佳答案
我终于让它工作了:D。这很hacky,但是可以用。
问题是,在xml-layouts中使用子屏幕会导致某些“代码魔术”。
将为该子屏幕启动一个新的 Activity/对话框,您没有直接访问权限。
要访问操作栏和主页/向上按钮的OnClickListener,您需要获取对PreferenceScreen的引用并获取其父对话框,以便访问操作栏及其主页/向上按钮。
这是在我的PreferenceFragment中完成的方法:
@Override
public void onCreate(Bundle savedInstanceState)
{
...
final PreferenceScreen preferenceScreen = (PreferenceScreen) findPreference(getString(R.string.keyPrefScreenDynamicWidgetDetails));
preferenceScreen.setOnPreferenceClickListener(new OnPreferenceClickListener()
{
public boolean onPreferenceClick(Preference preference)
{
preferenceScreen.getDialog().getActionBar().setDisplayHomeAsUpEnabled(true);
final Dialog dialog = preferenceScreen.getDialog();
View homeBtn = dialog.findViewById(android.R.id.home);
if (homeBtn != null)
{
OnClickListener dismissDialogClickListener = new OnClickListener()
{
@Override
public void onClick(View v)
{
dialog.dismiss();
}
};
// Prepare yourselves for some hacky programming
ViewParent homeBtnContainer = homeBtn.getParent();
// The home button is an ImageView inside a FrameLayout
if (homeBtnContainer instanceof FrameLayout) {
ViewGroup containerParent = (ViewGroup) homeBtnContainer.getParent();
if (containerParent instanceof LinearLayout) {
// This view also contains the title text, set the whole view as clickable
((LinearLayout) containerParent).setOnClickListener(dismissDialogClickListener);
} else {
// Just set it on the home button
((FrameLayout) homeBtnContainer).setOnClickListener(dismissDialogClickListener);
}
} else {
// The 'If all else fails' default case
homeBtn.setOnClickListener(dismissDialogClickListener);
}
}
return true;
}
});
...
}
以下链接为我提供了最终提示和代码来解决我的问题:
Action Bar Home Button not functional with nested PreferenceScreen