Android: PreferenceFragment 仅显示第一偏好

Android: PreferenceFragment only showing first preference

我在 Android 中加载 PreferenceFragment 时遇到问题。在这一点上,这是一个非常基本的实现,我想稍后在它工作时进行扩展。

问题是导航到 SettingsFragment.

后只显示第一个偏好

我没有收到任何错误,但是 logcat 向我展示了这个:

W/PathParser: Points are too far apart 4.000000596046461

我用谷歌搜索了这个,但没有任何有用的解决方案。

代码如下:

通过NavigationDrawer导航MainActivity

navigate() 是使用 FragmentManager

的通用函数
case R.id.nav_settings:
    navigate(new SettingsFragment(), "settingsFragment", false);
    break;

SettingsFragment

public class SettingsFragment extends PreferenceFragment implements SharedPreferences.OnSharedPreferenceChangeListener {
    public SettingsFragment() { }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }

    @Override
    public void onResume() {
        super.onResume();
        getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        getPreferenceScreen().getSharedPreferences().unregisterOnSharedPreferenceChangeListener(this);
    }

    @Override
    public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String s) { }
}

prefences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <SwitchPreference
        android:key="switch_notifications"
        android:title="Notifications"
        android:summary="Receive notifications"
        android:defaultValue="true" >

    </SwitchPreference>

    <CheckBoxPreference
        android:key="switch_notifications2"
        android:title="Notifications"
        android:summary="Receive notifications"
        android:defaultValue="true" >

    </CheckBoxPreference>

</PreferenceScreen>

截图

左:实际输出 |右:预览

提前致谢!

尼尔斯

抱歉,这可能不是答案(还不能发表评论),更像是一种见解。 只是想指出它在我的设备 (oneplus3) 上按预期工作,而且在 nexus 5 模拟器中似乎显示正确。

我的猜测:可能是父 activity 以某种方式限制了片段的输出?例如,如果我在父级上设置足够高的 paddingBottom,我会得到与左图相似的结果。

如果不是,则可能是一些奇怪的设备特定错误。也许您可以提供有关设备、屏幕尺寸、android 版本等的更多信息。

找到解决方案:

包含我的 FrameLayoutNestedScrollView 应该具有设置 android:fillViewport="true"。此外,FrameLayout 的高度应设置为 wrap_content.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="nmct.jaspernielsmichielhein.watchfriends.view.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <android.support.design.widget.CoordinatorLayout
        android:id="@+id/coordinator_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical" >

        <android.support.v4.widget.NestedScrollView
            android:id="@+id/frame_scrollview"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" >

            <FrameLayout
                android:id="@+id/fragment_frame"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                app:layout_behavior="@string/appbar_scrolling_view_behavior" >

            </FrameLayout>

        </android.support.v4.widget.NestedScrollView>

    </android.support.design.widget.CoordinatorLayout>

</RelativeLayout>