简体   繁体   中英

Nested PreferenceScreens under a dynamic list of preferences on Android

I'm trying to create a preferences screen with a dynamic list of entries, and when clicking each of them, I have another screen of preferences. As an example, think of a list of mail accounts and each one having their account settings available.

While I can create the nesting I want using just PreferenceScreens , this can't easily be scaled to multiple entries without creating the sub preferences structure in code for each one.

I see several different variations throughout the Android UI.

Is there a recomended way to create such a structure as this?

Possibilites include:

  1. Separate, independant activities
    Works but is messy in my opinion

  2. Nested, code created PreferenceScreens
    Pain in the ass for maintainence and it means the preferences are no longer stored as XML fragments

  3. Nested, inflated PreferenceScreens
    I can't find a way to expand another XML file into a sub tree

  4. One "sub" PreferenceScreen that is shown using setPreferenceScreen() for each one
    I can't find a way to hide the "template" PreferenceScreen and it breaks the navigation.

Sample XML:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:title="@string/prefs_title">
    <EditTextPreference android:key="add_console"
        android:title="@string/prefs_add_console"></EditTextPreference>
    <PreferenceCategory android:title="@string/prefs_consoles_title"
        android:key="list">
        <PreferenceScreen android:summary="http://cctv.icode.co.uk/"
            android:title="iCode Console">
        </PreferenceScreen>
        <PreferenceScreen android:summary="http://test.icode.co.uk/"
            android:title="Test Console">
        </PreferenceScreen>
    </PreferenceCategory>
    <PreferenceScreen android:title="Console (template)"
        android:key="console">
        <EditTextPreference android:title="@string/prefs_console_host"
            android:summary="@string/prefs_not_set" android:key="host"></EditTextPreference>
        <CheckBoxPreference android:title="@string/prefs_console_auth"
            android:summary="@string/prefs_console_auth_summary" android:key="auth"></CheckBoxPreference>
        <EditTextPreference android:shouldDisableView="true"
            android:title="@string/prefs_console_authuser" android:key="authuser"
            android:dependency="auth" android:summary="@string/prefs_not_set"></EditTextPreference>
        <EditTextPreference android:title="@string/prefs_console_authpass"
            android:key="authpass" android:dependency="auth" android:summary="@string/prefs_not_set"></EditTextPreference>
        <CheckBoxPreference android:title="@string/prefs_console_pair"
            android:summary="@string/prefs_console_pair_summary" android:key="pair"></CheckBoxPreference>
    </PreferenceScreen>
</PreferenceScreen>

I want the entries under list to be dynamic and show the console preferences under each.

All other ideas welcome.

Thanks

Have you tried to include dynamically XML files into the main one used for the preferences?

This technique is detailed in these 2 articles:

http://www.aslingandastone.com/2010/dynamically-changing-android-views/

http://android-developers.blogspot.com/2009/02/android-layout-tricks-2-reusing-layouts.html

They explain how to do what you were asking in your point 3:

Nested, inflated PreferenceScreens

Basically the idea would be to use this simple tag to include another XML file into an existing one:

<include
    android:id="@+id/layout_to_nest"
    layout="@layout/layout_to_nest" />

I don't know if you tried that yet but if not, it seems to be worth a try!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM