从XML中的另一个首选项屏幕引用一个单独的Android首选项屏幕

时间:2022-11-14 05:35:40

I have two Android Preference Screens defined in my Android app in XML.

我在我的Android应用程序中有两个用XML定义的Android偏好屏幕。

For example, Screen 1

例如,屏幕1

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="screen1">
    <PreferenceCategory android:title="Preferences">
        <CheckBoxPreference 
            android:defaultValue="true"
            android:title="test"
            android:key="test_pref"/>
    </PreferenceCategory>
</PreferenceScreen>

and Screen 2

和屏幕2

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="screen2">
    <CheckBoxPreference 
        android:key="checkbox" 
        android:title="Checkbox">
    </CheckBoxPreference>
</PreferenceScreen>

I would like screen 2 to be a separate screen to be accessible in its own right but I would also like its preferences to be a part of screen one also. Is there a simple way I can simply reference screen 2 from within screen 1? Or do I just need to essentially repeat the same preference stuff in a sub preference screen in Screen 1.

我希望屏幕2是一个独立的屏幕,可以自己访问,但我也希望它的首选项也是屏幕1的一部分。是否有一种简单的方法可以简单地从屏幕1中引用屏幕2 ?还是只需要在屏幕1的子首选项屏幕中重复相同的首选项。

2 个解决方案

#1


13  

I didn't find a way to "merge" both files directly in XML, but you could try to merge them using Java:

我没有找到直接用XML“合并”两个文件的方法,但是您可以尝试使用Java来合并它们:

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getPreferenceManager().setSharedPreferencesName(Settings.PREFERENCES_NAME);
    getPreferenceManager().setSharedPreferencesMode(Context.MODE_WORLD_READABLE);

    // add the first xml
    addPreferencesFromResource(R.xml.preferences_settings);
    // add another xml
    addPreferencesFromResource(R.xml.preferences_mail_settings);

    // do the things, that need to be done...
}

Good luck

祝你好运

Tom

汤姆

#2


7  

You can do this in XML with an Intent:

您可以在XML中使用意图:

<PreferenceScreen android:key="screen1">
  <PreferenceScreen android:key="screen2">
    <intent android:action="com.example.PREFERENCE_2" />
  </PreferenceScreen>
</PreferenceScreen>

AndroidManifest.xml:

AndroidManifest.xml:

<activity android:name="com.example.Preference2Activity">
  <intent-filter>
    <category android:name="android.intent.category.DEFAULT" />
    <action android:name="com.example.PREFERENCE_2" />
  </intent-filter>
</activity>

#1


13  

I didn't find a way to "merge" both files directly in XML, but you could try to merge them using Java:

我没有找到直接用XML“合并”两个文件的方法,但是您可以尝试使用Java来合并它们:

@Override
public void onCreate(final Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    getPreferenceManager().setSharedPreferencesName(Settings.PREFERENCES_NAME);
    getPreferenceManager().setSharedPreferencesMode(Context.MODE_WORLD_READABLE);

    // add the first xml
    addPreferencesFromResource(R.xml.preferences_settings);
    // add another xml
    addPreferencesFromResource(R.xml.preferences_mail_settings);

    // do the things, that need to be done...
}

Good luck

祝你好运

Tom

汤姆

#2


7  

You can do this in XML with an Intent:

您可以在XML中使用意图:

<PreferenceScreen android:key="screen1">
  <PreferenceScreen android:key="screen2">
    <intent android:action="com.example.PREFERENCE_2" />
  </PreferenceScreen>
</PreferenceScreen>

AndroidManifest.xml:

AndroidManifest.xml:

<activity android:name="com.example.Preference2Activity">
  <intent-filter>
    <category android:name="android.intent.category.DEFAULT" />
    <action android:name="com.example.PREFERENCE_2" />
  </intent-filter>
</activity>