一屏幕preference在细节preferenceFragme

一屏幕preference在细节preferenceFragme

本文介绍了如何加载在同一屏幕preference在细节preferenceFragment的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图做类似的设置preferences在Android的平板。

I am trying to do something like the settings preferences in android for tablet.

当我点击更多中的无线和网络preferences屏幕显示在右侧片段,比我碰VPN,在同一个片段的VPNpreferences屏幕打开。

When I click the "More" the the "Wireless & Networks" preferences screen is displayed on the right fragment, than if I touch the "VPN" , in the same fragment the "VPN" preferences screen is opened.

我怎么能这样做?

这是我的preference XML

this is my preference xml

<PreferenceScreen android:title="Title A">
        <PreferenceScreen android:title="TITLE B">
            <PreferenceCategory
                android:title="category">
                <ListPreference android:key="list"
                    android:title="list" android:entries="@array/list_vals"
                    android:entryValues="@array/list_vals1"
                    android:defaultValue="1" android:dialogTitle="list title"
                    android:negativeButtonText="cancel" />
            </PreferenceCategory>
        </PreferenceScreen>
</PreferenceScreen>

现在的问题是,我不希望与标题B嵌套preference显示屏整屏的我只是想在正确的片段的地方...

The problem is I do not want the nested preference screen with title B to be loaded on the whole screen I just want to be loaded in the place of the right fragment...

注:我用这个文档创建我的例子到目前为止,<一个href="http://developer.android.com/guide/topics/ui/settings.html#$p$pferenceHeaders">http://developer.android.com/guide/topics/ui/settings.html#$p$pferenceHeaders

NOTE: that I used this documentation to create my example so far http://developer.android.com/guide/topics/ui/settings.html#PreferenceHeaders

修改*

这是我的活动

public class SettingsActivity extends PreferenceActivity {
    @Override
    public void onBuildHeaders(List<Header> target) {
        loadHeadersFromResource(R.xml.preference_headers, target);
    }
}

我没有一个布局,所以我怎么能知道什么是我的主人片段的id,什么是我的细节片段ID?

I do not have an layout, so how can I know what is my master fragment id and what is my detail fragment id ?

推荐答案

简单例子,定义了一个 preferenceActivity ,并提供主要类别一个头文件:

Short example, define a PreferenceActivity, and provide a headers file for main categories:

SettingsActivity

public class SettingsActivity extends PreferenceActivity {
    @Override
    public void onBuildHeaders(List<Header> target) {
        super.onBuildHeaders(target);
        loadHeadersFromResource(R.xml.headers,target);
    }
}

文件 /res/xml/headers.xml

<?xml version="1.0" encoding="utf-8"?>

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
    <header android:title="Preference Header"
        android:fragment="com.example.SettingsDemo.PrefFragment"/>
</preference-headers>

创建一个 preferenceFragment 中引用的安卓片段以上,提供自己的preferences文件:

Create a PreferenceFragment as referenced in android:fragment above, provide its own preferences file:

prefFragment

public class PrefFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.prefs);
    }
}

文件 /res/xml/$p$pfs.xml

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
 <PreferenceCategory android:title="Preferences">
 <SwitchPreference android:title="Preference" />
    <PreferenceScreen android:title="Sub Preferences"
        android:fragment="com.example.SettingsDemo.SubPrefFragment"/>
 </PreferenceCategory>
</PreferenceScreen>

创建一个 preferenceFragment 中引用的安卓片段以上,提供自己的preferences文件太多:

Create a child PreferenceFragment as referenced in android:fragment above, provide its own preferences file too:

子prefFragment

public class SubPrefFragment extends PreferenceFragment {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.sub_prefs);
    }
}

文件 /res/xml/sub_$p$pfs.xml

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    <PreferenceCategory android:title="Sub Preferences">
        <CheckBoxPreference android:title="Sub Preference"/>
    </PreferenceCategory>
</PreferenceScreen>

就是这样。该 preferenceActivity 将自动显示所有子片段。

That's it. The PreferenceActivity will automatically show all the child fragments.

结果:

主要 preferenceFragment

Main PreferenceFragment :

由子替换 preferenceFragment ,同样的标题下:

Replaced by sub PreferenceFragment , under same header:

这篇关于如何加载在同一屏幕preference在细节preferenceFragment的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 20:58