簡體   English   中英

如何在android中制作一個簡單的設置頁面?

[英]How to make a simple settings page in android?

我是“Android-App-Dev”場景的新手,有一個問題:

如何輕松地為我的應用程序制作一個干凈整潔的設置頁面?

有一些標題和一些大按鈕,您可以通過 Tab 鍵轉到新頁面。

我正在使用 Android Studio 並且知道如何創建新頁面、類等。

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

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

PreferenceActivity實際上在 API 級別 29 ( source ) 中已棄用:

此類在 API 級別 29 中已棄用。使用 AndroidX 首選項庫在所有設備上實現一致的行為。 有關使用 AndroidX 首選項庫的更多信息,請參閱設置。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM