ScrollView > LinearLayout > PreferenceFragment 的高度为零

2024-04-06

我有一个具有 ScrollView 和垂直 LinearLayout 的活动,该 Activity 有两个片段,它们是 PreferenceFragment 实例,如以下布局文件所示:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="wrap_content"
android:layout_width="match_parent"
>

<LinearLayout
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    tools:context="com.foo.app.SettingsActivity">

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.foo.app.SettingsFragment"
        android:id="@+id/fragment_settings"/>

    <fragment
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:name="com.foo.app.NotificationSettingsFragment"
        android:id="@+id/fragment_notification_settings"/>

</LinearLayout>

问题是这两个片段仅显示其 PreferenceCategory 标题,而实际的片段 UI 高度为零且不可见。奇怪的是,可以单独滚动每个片段并查看丢失的片段 UI。就好像每个 Fragment 都在 ScrollView 内。

我期望的是调整两个片段的大小以包裹其内容,并且有一个垂直滑块来滚动包含两个片段的 LinearLayout。

如果相关,这两个片段会扩展 android.preference.PreferenceFragment 并且不在布局文件中定义其布局。相反,他们加载其首选项 UI,如下所示:

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

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

TIA 寻求您的帮助。


删除外部 ScrollView。我遇到了同样的问题,这为我解决了。

我还没有在文档中找到它,但显然 PreferenceFragment 在 PreferenceScreen 周围提供了自己的 ScrollView。不知何故,这导致了一个足以显示第一个元素(例如类别标题)的wrap_content高度。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

ScrollView > LinearLayout > PreferenceFragment 的高度为零 的相关文章

随机推荐