【问题标题】:Skip the headers in PreferenceActivity when there's only one header当只有一个标头时跳过 PreferenceActivity 中的标头
【发布时间】:2012-06-03 21:11:12
【问题描述】:

我在我的应用程序中添加了首选项标题,以便首选项屏幕在 Honeycomb 和平板电脑大小的 ICS 上看起来不会损坏。但是,我目前只有一个标题,因此您必须单击标题屏幕,在手机大小的设备上只有一个条目。有没有一种简单的方法可以告诉android在只有一个标题时跳过标题屏幕,但仍然在大屏幕上显示它?

股票通讯录应用似乎成功地做到了这一点,但我浏览了它的 source 并无法弄清楚它是如何做到的。

【问题讨论】:

  • 在联系人应用程序中,默认的 PreferenceFragment 在从菜单启动设置活动时指定,使用与@jdr88 提供的相同技术。欲了解更多信息,请转到 here 和 Ctrl-F 以获取 settingsAreMultiPane

标签: android preferenceactivity


【解决方案1】:

您可以通过将您的 PreferenceFragments 之一设置为默认值来跳过标题。

当您查看PreferenceActivity.java 源时,您会发现以下两个额外内容:

/**
 * When starting this activity, the invoking Intent can contain this extra
 * string to specify which fragment should be initially displayed.
 */
public static final String EXTRA_SHOW_FRAGMENT = ":android:show_fragment";

/**
 * When starting this activity, the invoking Intent can contain this extra
 * boolean that the header list should not be displayed.  This is most often
 * used in conjunction with {@link #EXTRA_SHOW_FRAGMENT} to launch
 * the activity to display a specific fragment that the user has navigated
 * to.
 */
public static final String EXTRA_NO_HEADERS = ":android:no_headers";

现在只需将这两个额外添加到正在调用您的 PrefenceActivity 的意图中,并指定默认情况下应显示的 PreferenceFragment,如下所示:

Intent intent = new Intent( this, Preferences.class );
intent.putExtra( PreferenceActivity.EXTRA_SHOW_FRAGMENT, PreferencesFragment.class.getName() );
intent.putExtra( PreferenceActivity.EXTRA_NO_HEADERS, true );

【讨论】:

  • 这就是我想要的!谢谢。我发现只使用 EXTRA_SHOW_FRAGMENT 意味着标题在平板电脑上仍然可见,但它会直接跳转到手机上的设置片段,这正是我想要的。
  • 您能否阐明“将您的 PreferenceFragments 之一设置为默认值”的意思?我的版本如下。我没有看到在 XML 中指定默认值的替代方法。 (我检查了 XML 解析的来源。)
  • 嗨。我收到错误“此活动的片段无效”。我该怎么办?
【解决方案2】:

利用 jenzz 提到的EXTRA_SHOW_FRAGMENT,你可以像这样操作Activity的Intent:

@Override
protected void onCreate(Bundle savedInstanceState) {
  // By default, show MainPreferences
  Intent intent = getIntent();
  if (intent.getStringArrayExtra(EXTRA_SHOW_FRAGMENT) == null) {
    getIntent().putExtra(EXTRA_SHOW_FRAGMENT, MainPreferences.class.getName());
  }

  super.onCreate(savedInstanceState);
}

【讨论】:

    【解决方案3】:

    您可以在活动中删除此代码。

         public void onBuildHeaders(List<Header> target) {
            loadHeadersFromResource(R.xml.pref_general, target);
        }
    

    并替换您的片段:

    public class SettingsActivity extends AppCompatPreferenceActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
               getFragmentManager().beginTransaction().replace(android.R.id.content,
                    new GeneralPreferenceFragment()).commit();
    
        }
    
        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        public static class GeneralPreferenceFragment extends PreferenceFragment {
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                addPreferencesFromResource(R.xml.pref_general);
    
            }
    
        }
    
    }
    

    【讨论】:

      【解决方案4】:

      我不知道你是否可以专门跳过标题,但这就是我所做的。

      我创建了 2 个类,一个用于超大屏幕尺寸,一个用于其余的。 EditPreferences.class 加载我正常的preferences.xml,EditPreferencesXLarge.class 加载preference-headers xml。

      public boolean onOptionsItemSelected(MenuItem item) {
          final int SCREENLAYOUT_SIZE_XLARGE = 4;
          final int HONEYCOMB = 11;
          int screenSize = getResources().getConfiguration().screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK;
      
          switch(item.getItemId())
          {
              case R.id.item_prefs:
                  if (Build.VERSION.SDK_INT < HONEYCOMB) {
                      startActivity(new Intent(this, EditPreferences.class));
                  }
                  else if (screenSize < SCREENLAYOUT_SIZE_XLARGE) {
                      startActivity(new Intent(this, EditPreferences.class));
                  }
                  else {
                      startActivity(new Intent(this, EditPreferencesXLarge.class));
                  }
      
                  return true;
          }
      
          return (super.onOptionsItemSelected(item));
      }
      

      【讨论】:

      • 您确实可以跳过标题。我在 onBuildHeaders(...) 中使用 if 语句仅在超大屏幕上加载标题。否则,我使用 onPostCreate() 将内容替换为片段。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-04-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多