【问题标题】:How to make a simple settings page in android?如何在android中制作一个简单的设置页面?
【发布时间】:2014-10-11 21:46:30
【问题描述】:

我是“Android-App-Dev”-Scene 的新手,有一个问题:

如何轻松地为我的应用制作一个外观整洁的设置页面?

上面有一些标题和一些大按钮,您可以通过标签转到新页面。

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

【问题讨论】:

标签: java android android-studio settings


【解决方案1】:

截至 2019 年,推荐的方法是使用 AndroidX Preference Library

PreferenceActivity 实际上在 API 级别 29 (source) 中已被弃用:

此类在 API 级别 29 中已弃用。 使用 AndroidX 首选项库在所有设备上实现一致的行为。有关使用 AndroidX 首选项库的更多信息,请参阅设置。

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

【讨论】:

  • 我喜欢我被否决的方式,即使我实际上是在引用官方文档。
  • 确实如此。我仍然看到人们引用已弃用的 api。
【解决方案2】:

使用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。

【讨论】:

  • 请帮我一个忙(因为我是新手)并写下这些代码应该写在哪里(在哪些文件中)。
  • 另外,按钮应该如何调用/打开该页面。
猜你喜欢
  • 2017-06-30
  • 2014-03-07
  • 2013-06-25
  • 2012-12-19
  • 1970-01-01
  • 2011-09-16
  • 1970-01-01
  • 1970-01-01
  • 2023-03-12
相关资源
最近更新 更多