[android]隐藏PreferenceCategory和Preference的方法

时间:2025-01-27 19:08:02

1. 隐藏PreferenceCategory,也就是把这个种类下的子菜单和本分类的title都隐藏,方法如下:

xml文件:

<PreferenceScreen xmlns:andro>
    <PreferenceCategory android:key="pref_key_title_display_settings" android:title="@string/pref_title_display_settings">
        <Preference android:key="pref_key_chat_wallpaper_for_chat" android:title="@string/pref_chat_wallpaper" /> 
    </PreferenceCategory>

</PreferenceScreen>

Java代码:

    public static final String CHAT_KEY_DISPLAY = "pref_key_title_display_settings";

    public void removeDisplayCategory() {
        PreferenceCategory displayOptions = (PreferenceCategory) findPreference(CHAT_KEY_DISPLAY);
        getPreferenceScreen().removePreference(displayOptions);
    }


2.隐藏Preference,也就是只隐藏一个PreferenceCategory下的一个子菜单,方法如下:

xml文件:

<PreferenceScreen xmlns:andro>
    <PreferenceCategory android:title="@string/pref_title_display_preference_settings"
        android:key="pref_key_display_preference_settings">
        <Preference android:key="pref_key_chat_wallpaper"
            android:title="@string/pref_chat_wallpaper" />
        <Preference android:key="pref_key_message_font_size"
            android:title="@string/pref_title_message_font_size"
            android:summary="@string/pref_summary_message_font_size" />
        <CheckBoxPreference android:defaultValue="true"
            android:key="pref_key_auto_rotation" android:title="@string/pref_auto_rotation" />
        <CheckBoxPreference android:defaultValue="true"
            android:key="pref_key_show_email_address" android:title="@string/pref_show_email_address"
            android:summary="@string/pref_summary_show_email_address" />
    </PreferenceCategory>

</PreferenceScreen>

Java代码:

    public static final String DISPLAY_PREFERENCE = "pref_key_display_preference_settings";
    public static final String GENERAL_CHAT_WALLPAPER = "pref_key_chat_wallpaper";

    public void removeGeneralChatWallpaper() {
        PreferenceCategory displayOptions = (PreferenceCategory) findPreference(DISPLAY_PREFERENCE);
        (findPreference(GENERAL_CHAT_WALLPAPER));
    }