gpt4 book ai didi

显示上一个 fragment 的 Android PreferencesActivity

转载 作者:行者123 更新时间:2023-11-30 01:23:39 27 4
gpt4 key购买 nike

我根据示例 here 通过扩展 PreferencesActivity 创建了一个 Preferences 屏幕.

第一个屏幕显示正常,但当我点击查看第二个屏幕时,我看到它们一个叠一个。因此,根据其他一些线程的建议,我将第二个 fragment 的背景更改为黑色。

虽然我再也看不到它们了,但它仍然有效。但是我只看到第一个,除了标题。

第一个屏幕是这样的:

enter image description here

第二个看起来像这样:

enter image description here

只有标题行发生了变化,其余的保持不变。

这是我 Activity 中的代码:

public class SettingsActivity  extends PreferenceActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
getFragmentManager().beginTransaction().replace(android.R.id.content, new PreferencesMain()).commit();


}

/**
* 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 PreferencesMain extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences);
}

@Override
public void onStart() {
super.onStart();
View view = getView();
view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
}
}

/**
* 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 PreferencesNotifications extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Load the preferences from an XML resource
addPreferencesFromResource(R.xml.preferences_notifications);

}

@Override
public void onStart() {
super.onStart();
View view = getView();
view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.setBackgroundColor(ResourcesCompat.getColor(getResources(),android.R.color.black, null));
}
}

@Override
protected boolean isValidFragment (String fragmentName) {
return true;
}

}

首选项.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory
android:title="@string/settings_notifications">

<!-- 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="activities.SettingsActivity$PreferencesNotifications"
android:title="@string/settings_notifications_managePushTitle"
android:summary="@string/settings_notifications_managePushSummary">
android:background="@android:color/black"
<!-- Arbitrary key/value pairs can be included for fragment arguments -->
<extra android:name="someKey" android:value="somePrefValue" />
</PreferenceScreen>

</PreferenceCategory>

preferences_notifications.xml

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

<PreferenceCategory
android:title="@string/settings_notifications_managePushHeader">

<CheckBoxPreference
android:key="checkbox_preference"
android:title="@string/settings_notifications_managePushEntry1Title"
android:summary="@string/settings_notifications_managePushEntry1Summary" />
</PreferenceCategory>

最佳答案

添加一个header.xml来管理设置,并使用onBuildHeaders方法。

资源headers.xml:

<preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
<header android:title="Digle"
android:fragment="activities.SettingsActivity$PreferencesMain"/>
</preference-headers>

SettingsActivity:

public class SettingsActivity extends PreferenceActivity {

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

// Remove all code in here
}

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

...
}

编辑:另一种方法是使用 subscreens .您必须将 Preference 对象组放在 PreferenceScreen 中。

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="Settings"
android:title="@string/app_name">
<PreferenceCategory
android:title="@string/settings_notifications">

<PreferenceScreen
android:background="@android:color/black"
android:summary="@string/settings_notifications_managePushSummary"
android:title="@string/settings_notifications_managePushTitle">

<PreferenceCategory
android:title="@string/settings_notifications_managePushHeader">

<CheckBoxPreference
android:key="checkbox_preference"
android:summary="@string/settings_notifications_managePushEntry1Summary"
android:title="@string/settings_notifications_managePushEntry1Title"/>

</PreferenceCategory>

</PreferenceScreen>

</PreferenceCategory>

</PreferenceScreen>

关于显示上一个 fragment 的 Android PreferencesActivity,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36742872/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com