如何在安卓系统中创建一个简单的设置页面?

6

我是Android应用开发的新手,有一个问题:

如何轻松地为我的应用程序创建一个干净漂亮的设置页面?

通常有一些标题和大按钮,你可以点击它们以进入新页面。

我正在使用Android Studio,并知道如何创建新页面、类等。


1
http://developer.android.com/guide/topics/ui/settings.html - CommonsWare
嗨,我写了一个库,可以轻松设置偏好屏幕。请查看:https://github.com/marcauberer/simple-settings - ChilliBits
2个回答

10
截至2019年,推荐使用AndroidX偏好设置库来实现此功能。
在API Level 29(source)中,PreferenceActivity已经被弃用:

此类已在API级别29中弃用。 使用AndroidX Preference Library可实现所有设备上的一致行为。有关使用AndroidX Preference Library的更多信息,请参见设置。

有关工作的最小示例,请参阅官方文档中this示例。

2
我喜欢即使我字面上引用了官方文档,也会被踩的感觉。 - Josip Domazet
1
没错。我仍然看到人们在引用已弃用的API。 - thatGuy
1
9到1还不错,但这里又加上了+1 :) - Arlen Beiler

6

使用PreferenceActivity

以下是来自开发者网站的示例代码:

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 资源描述要显示的头部以及与其相关联的片段。它的格式如下:

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

<header android:fragment="com.example.android.apis.preference.PreferenceWithHeaders$Prefs2Fragment"
        android:icon="@drawable/ic_settings_display"
        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_settings_display"
        android:title="Intent"
        android:summary="Launches an Intent.">
    <intent android:action="android.intent.action.VIEW"
            android:data="http://www.android.com" />
</header>

首个标题由Prefs1Fragment显示,它从以下XML资源填充自身:

<PreferenceCategory
        android:title="@string/inline_preferences">

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

</PreferenceCategory>

<PreferenceCategory
        android:title="@string/dialog_based_preferences">

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

    <ListPreference
            android:key="list_preference"
            android:title="@string/title_list_preference"
            android:summary="@string/summary_list_preference"
            android:entries="@array/entries_list_preference"
            android:entryValues="@array/entryvalues_list_preference"
            android:dialogTitle="@string/dialog_title_list_preference" />

</PreferenceCategory>

<PreferenceCategory
        android:title="@string/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.android.apis.preference.PreferenceWithHeaders$Prefs1FragmentInner"
            android:title="@string/title_fragment_preference"
            android:summary="@string/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="@string/title_intent_preference"
            android:summary="@string/summary_intent_preference">

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

    </PreferenceScreen>

</PreferenceCategory>

<PreferenceCategory
        android:title="@string/preference_attributes">

    <CheckBoxPreference
            android:key="parent_checkbox_preference"
            android:title="@string/title_parent_preference"
            android:summary="@string/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="@string/title_child_preference"
            android:summary="@string/summary_child_preference" />

</PreferenceCategory>

请注意,这个XML资源包含一个首选项屏幕,其中包含另一个片段,即此处实现的Prefs1FragmentInner。这允许用户遍历偏好设置的层次结构;按返回键将弹出堆栈中的每个片段,以返回上一个偏好设置。
有关实现片段本身的信息,请参见PreferenceFragment。

请帮我一个忙(因为我是新手),告诉我这些代码应该写在哪些文件中。 - T.Todua
还有,一个按钮应该如何调用/打开那个页面。 - T.Todua
请提供其他示例:http://stackoverflow.com/questions/41588924/need-example-of-button-for-opening-settings-page-android-studio?noredirect=1#comment70382487_41588924 - T.Todua

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接