PreferenceActivity与PreferenceFragment的理解

     PreferenceActivity用来呈现一种层次化的preferences布局,所有继承自它的activity可以显示一个或多个headers,每一个header都与一个PreferenceFragment相关。PreferenceActivity有两种表现方式:

1.小型屏幕上,只显示headers列表,选择一个header会只显示该header对应的PreferenceFragment;

2.大屏幕上,会把headers列表和当前的Fragment一起显示,选择一个header会切换到该header对应的PreferenceFragment。PreferenceActivity的子类需要实现onBuildHeaders(List)来生成headers列表。

看下面的例子:

package com.example.preferencefragmentdemos;

import java.util.List;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
import android.preference.PreferenceManager;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.widget.Button;
import android.widget.Toast;

public class PreferenceWithHeaders extends PreferenceActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Add a button to the header list.
        if (hasHeaders()) {
            Button button = new Button(this);
            button.setText("Some action");
            setListFooter(button);
        }
    }

    /**
     * Populate the activity with the top-level headers.
     */
    @Override
    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preference_headers, target);
    }

    /**
     * This fragment shows the preferences for the first header.
     */
    public static class Prefs1Fragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Make sure default values are applied.  In a real app, you would
            // want this in a shared function that is used to retrieve the
            // SharedPreferences wherever they are needed.
            //PreferenceManager.setDefaultValues(getActivity(),
                    //R.xml.advanced_preferences, false);

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.fragmented_preferences);
        }
    }

    /**
     * This fragment contains a second-level set of preference that you
     * can get to by tapping an item in the first preferences fragment.
     */
    public static class Prefs1FragmentInner extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Can retrieve arguments from preference XML.
            Log.i("args", "Arguments: " + getArguments());

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.fragmented_preferences_inner);
        }
    }

    /**
     * This fragment shows the preferences for the second header.
     */
    public static class Prefs2Fragment extends PreferenceFragment {
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            // Can retrieve arguments from headers XML.
            Log.i("args", "Arguments: " + getArguments());

            // Load the preferences from an XML resource
            addPreferencesFromResource(R.xml.preference_dependencies);
        }
    }
}

preference_headers.xml

<?xml version="1.0" encoding="utf-8"?>
<preference-headers
        xmlns:android="http://schemas.android.com/apk/res/android">

    <header android:fragment="com.example.preferencefragmentdemos.PreferenceWithHeaders$Prefs1Fragment"
            android:icon="@drawable/ic_launcher"
            android:title="Prefs 1"
            android:summary="An example of some preferences." />

    <header android:fragment="com.example.preferencefragmentdemos.PreferenceWithHeaders$Prefs2Fragment"
            android:icon="@drawable/ic_launcher"
            android:title="Prefs 2"
            android:summary="Some other preferences you can see.">
        <!-- Arbitrary key/value pairs can be included with a header as
             arguments to its fragment. -->
        <extra android:name="someKey" android:value="someHeaderValue" />
    </header>

    <header android:icon="@drawable/ic_launcher"
            android:title="Intent"
            android:summary="Launches an Intent.">
        <intent android:action="android.intent.action.VIEW"
                android:data="http://www.android.com" />
    </header>

</preference-headers>

fragmented_preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
        xmlns:android="http://schemas.android.com/apk/res/android">

    <PreferenceCategory
            android:title="inline_preferences">

        <CheckBoxPreference
                android:key="checkbox_preference"
                android:title="title_checkbox_preference"
                android:summary="summary_checkbox_preference" />

    </PreferenceCategory>

    <PreferenceCategory
            android:title="dialog_based_preferences">

        <EditTextPreference
                android:key="edittext_preference"
                android:title="title_edittext_preference"
                android:summary="summary_edittext_preference"
                android:dialogTitle="dialog_title_edittext_preference" />

    </PreferenceCategory>

    <PreferenceCategory
            android:title="launch_preferences">

        <!-- This PreferenceScreen tag sends the user to a new fragment of
             preferences.  If running in a large screen, they can be embedded
             inside of the overall preferences UI. -->
        <PreferenceScreen
                android:fragment="com.example.preferencefragmentdemos.PreferenceWithHeaders$Prefs1FragmentInner"
                android:title="title_fragment_preference"
                android:summary="summary_fragment_preference">
            <!-- Arbitrary key/value pairs can be included for fragment arguments -->
            <extra android:name="someKey" android:value="somePrefValue" />
        </PreferenceScreen>

        <!-- This PreferenceScreen tag sends the user to a completely different
             activity, switching out of the current preferences UI. -->
        <PreferenceScreen
                android:title="title_intent_preference"
                android:summary="summary_intent_preference">

            <intent android:action="android.intent.action.VIEW"
                    android:data="http://www.baidu.com" />

        </PreferenceScreen>

    </PreferenceCategory>

    <PreferenceCategory
            android:title="preference_attributes">

        <CheckBoxPreference
                android:key="parent_checkbox_preference"
                android:title="title_parent_preference"
                android:summary="summary_parent_preference" />

        <!-- The visual style of a child is defined by this styled theme attribute. -->
        <CheckBoxPreference
                android:key="child_checkbox_preference"
                android:dependency="parent_checkbox_preference"
                android:layout="?android:attr/preferenceLayoutChild"
                android:title="title_child_preference"
                android:summary="summary_child_preference" />

    </PreferenceCategory>

</PreferenceScreen>

preference_dependencies.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
     <EditTextPreference
                android:key="edittext_preference"
                android:title="title_edittext_preference"
                android:summary="summary_edittext_preference"
                android:dialogTitle="dialog_title_edittext_preference" />

</PreferenceScreen>

fragmented_preferences_inner.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
    <CheckBoxPreference 
        android:key="auto_save"
        android:title="自动保存进度"
        android:summary="用来自动保存进度"
    />

</PreferenceScreen>


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值