显示上行按钮的动作条在子屏幕preferences [英] Show up-Button in actionBar in subscreen preferences

查看:389
本文介绍了显示上行按钮的动作条在子屏幕preferences的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我实现了我的preferences一样显示在官方的指导方针。

I've implemented my preferences like shown in the official guidelines.

我有一个preferenceActivity创造了preferenceFragment是这样的:

I have a PreferenceActivity which creates the PreferenceFragment like this:

 @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加载从资源preferences,它们含有preference子屏幕是这样的:

The PreferenceFragment loads the preferences from the resources and they contain a preference subscreen like this:

<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>

这工作得很好,到目前为止,但现在我想有一个上行按钮的动作条的preferences子屏幕显示时。任何想法如何实现这一目标?

This works well so far, but now I'd like to have an up-Button in the actionBar when the preferences subscreen is shown. Any idea how to accomplish that?

我已经尝试设置 setDisplayHomeAsUpEnabled(真)在我的活动,但随后向上按钮只在主preferences所示(它不应该)而不是在子屏幕。

I have tried to set setDisplayHomeAsUpEnabled(true) in my activity but then the up-Button is only shown in the main preferences (where it should not) and not in the subscreen.

我不知道,即使是在官方的文档的子屏幕显示不存在活跃上行按钮:

I'm wondering that even in the official docs the subscreen is shown without an active up-Button:

链接到文档:设置

任何帮助是非常欢迎的。

Any help is really welcome

推荐答案

我终于得到它的工作:D。这是相当哈克,但它的工作原理。

I finally got it to work :D. It's quite hacky but it works.

的问题是,使用XML-布局结果subscreens在一些'code魔力'。 开始的子屏幕的新活动/对话框,你不必直接访问它。

The problem is, that using subscreens in xml-layouts results in some 'code magic'. A new activity/dialog is started for the subscreen and you don't have direct access to it.

要访问该动作条和家庭/上行按钮,你需要得到引用你的preferenceScreen,并得到其母公司对话,以访问动作条和家庭/向上按钮的OnClickListener

To get access to the actionbar and the OnClickListener of the home/up-button you need to get a reference to your PreferenceScreen and get its parent Dialog in order to access the actionbar and its home/up button.

这是它是如何在我的preferenceFragment完成的:

This is how it is done inside my 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;
       }
  });
...

}

下面的链接给了我最后的提示和code,以解决我的问题:

Following link gave me the final hints and code to solve my problem:

<一个href="http://stackoverflow.com/questions/16374820/action-bar-home-button-not-functional-with-nested-$p$pferencescreen">Action酒吧主页按钮不起作用嵌套preferenceScreen

这篇关于显示上行按钮的动作条在子屏幕preferences的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆