我有一个包含ScrollView的活动,其中包含一个垂直LinearLayout,该LinearLayout具有两个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);
}
感谢您的帮助。