ScrollView> LinearLayout> PreferenceFragment为零高度

Far*_*jmi 6 android scrollview android-linearlayout preferencefragment

我有一个活动,它有一个带有垂直LinearLayout的ScrollView,它有两个片段,它们是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>
Run Code Online (Sandbox Code Playgroud)

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

我期望的是两个片段的大小可以包装它们的内容,并且有一个垂直滑块可以滚动包含两个片段的LinearLayout.

如果它是相关的,这两个片段扩展android.preference.PreferenceFragment并且不在布局文件中定义它们的布局.相反,他们加载他们的Preference UI如下:

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

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.pref_notification);
}
Run Code Online (Sandbox Code Playgroud)

TIA的帮助.

小智 6

删除外部ScrollView.我有同样的问题,这解决了我.

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

  • 我有多个片段构成自定义的SettingsActivity.如果我删除外部ScrollView,那么我会在每个自定义设置片段中获得单独的垂直滚动条.这是不可取的,因为我只想要一个垂直的SCroll栏.因此上述解决方案无法满足我的需求.谢谢你的帮助. (3认同)
  • 任何解决方案 我处于类似的情况,我有正常的活动,在其布局中有一些图片,按钮,在顶部和底部以及片段或两个在中间.如果这个片段的大小是推荐的0dp,它们不会占用他们需要的空间,但它们可以滚动(单独).我想要实现的是实际上一次滚动整个屏幕,这个片段占用了他们需要的空间(因此它们不能单独滚动整个屏幕).目前我只是将这些片段的高度设置为固定大小(例如300dp).根本不优雅;( (2认同)

小智 5

这个问题已经很老了,但万一其他人偶然发现了这个问题,我已经设法找到了解决方案.就像@Ewoks提到的那样,我也必须适应一个固定的高度(这并不总能很好地适应不同设备的dpi).

但是在这段代码的帮助下,我设法使高度动态,这很好地包装了内容.

在你的PreferenceFragment课堂上做如下:

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

    if (getView() != null) {

        ListView listView = (ListView) getView().findViewById(android.R.id.list);
        Adapter adapter = listView.getAdapter();

        if (adapter != null) {
            int height = 0;
            //int height = listView.getPaddingTop() + listView.getPaddingBottom();

            for (int i = 0; i < adapter.getCount(); i++) {
                View item = adapter.getView(i, null, listView);

                item.measure(0, 0);
                height += item.getMeasuredHeight();
            }

            FrameLayout frame = (FrameLayout) getActivity().findViewById(R.id.content_frame); //Modify this for your fragment

            ViewGroup.LayoutParams param = frame.getLayoutParams();
            param.height = height + (listView.getDividerHeight() * adapter.getCount());
            frame.setLayoutParams(param);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

希望这有助于某人!