只有一个标头时,跳过PreferenceActivity中的标头

问题描述:

我在我的应用程序中添加了首选项标题,以便首选项屏幕在Honeycomb和平板电脑大小的ICS上不会出现损坏.但是,此刻我只有一个标题,因此您必须在电话尺寸的设备上单击标题只有一个条目的标题屏幕.有没有一种简单的方法告诉Android在只有一个标头的情况下跳过标头屏幕,而仍然在大屏幕上显示它?

I added preference-headers to my app so that the preference screen would not look broken on Honeycomb and tablet sized ICS. However, I only have one header at the moment so you have to click through a header screen with only one entry on phone sized devices. Is there an easy way to tell android to skip the header screen when there's only one header, but to still show it on large screens?

似乎股票联系人应用程序成功完成了此操作,但我浏览了其源代码,无法弄清楚它正在这样做.

It seems that the stock Contacts app does this successfully but I've browsed through its source and can't figure out how it is doing it.

您可以通过将其中一个PreferenceFragments设置为默认值来跳过标题.

You can skip the headers by setting one of your PreferenceFragments as default.

当您查看 PreferenceActivity.java 时>来源,您会发现这两个额外功能:

When you take a look at the PreferenceActivity.java source, you will find these two extras:

/**
 * 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,如下所示:

Now simply add these two extras to the intent which is invoking your PrefenceActivity and specify the PreferenceFragment which should be shown by default as follows:

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