当只有一个标题时,在PreferenceActivity中跳过标题

Fre*_*Dug 25 android preferenceactivity

我在我的应用程序中添加了preference-headers,以便在Honeycomb和平板电脑大小的ICS上看起来不会出现偏好屏幕.但是,我目前只有一个标题,因此您必须在电话大小的设备上只有一个条目点击标题屏幕.有没有一种简单的方法可以告诉android跳过标题屏幕,当只有一个标题,但仍然在大屏幕上显示它?

似乎股票联系人应用程序成功完成了这一点,但我浏览了它的来源,无法弄清楚它是如何做到的.

jen*_*nzz 61

您可以通过将其中一个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";
Run Code Online (Sandbox Code Playgroud)

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

Intent intent = new Intent( this, Preferences.class );
intent.putExtra( PreferenceActivity.EXTRA_SHOW_FRAGMENT, PreferencesFragment.class.getName() );
intent.putExtra( PreferenceActivity.EXTRA_NO_HEADERS, true );
Run Code Online (Sandbox Code Playgroud)

  • 这就是我在寻找的东西!谢谢.我发现只使用EXTRA_SHOW_FRAGMENT意味着标题在平板电脑上仍然可见,但它会直接跳到手机上的设置片段,这正是我想要的. (3认同)
  • 你好.我收到错误"此活动的无效片段".我该怎么办? (2认同)