android - ScrollView > LinearLayout > PreferenceFragment 为零高度

标签 android scrollview android-linearlayout preferencefragment

我有一个 Activity ,它有一个带有垂直 LinearLayout 的 ScrollView,它有两个 fragment ,它们是 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>

问题在于,这两个 fragment 只显示了它们的 PreferenceCategory 标题,而实际的 fragment UI 高度为零且不可见。奇怪的是,可以单独滚动每个 fragment 并看到丢失的 fragment UI。就好像每个 Fragment 都在一个 ScrollView 中。

我期望的是两个 fragment 的大小可以包裹它们的内容,并且有一个垂直 slider 可以滚动包含两个 fragment 的 LinearLayout。

在相关的情况下,这两个 fragment 扩展了 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 高度刚好足以显示第一个元素(例如类别标题)。

关于android - ScrollView > LinearLayout > PreferenceFragment 为零高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22973089/

相关文章:

java - 如何在 Android 中转换日期和时间

java - 显示 HTML 格式化字符串

android - ScrollView 和 LinearLayout 的困难

android - ListView 需要 2 个 setSelection 才能滚动到项目

机器人 : developer options hidden

Android Studio - ScrollView 在键盘打开时移出屏幕

java - 如何在文本框下方显示三个标签和微调器?

android - 如何保持两个TextInputLayout单行宽度一致

Android 线性布局权重以编程方式