[android]隐藏PreferenceCategory和Preference的方法

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

xml文件:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <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:android="http://schemas.android.com/apk/res/android">
    <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);
        displayOptions.removePreference(findPreference(GENERAL_CHAT_WALLPAPER));
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值