Skip the headers in PreferenceActivity when there's only one header

前端 未结 4 1778
无人共我
无人共我 2020-12-24 07:48

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

4条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-24 08:28

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

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

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

提交回复
热议问题