ScrollView>线性布局PreferenceFragment为零高度 [英] ScrollView > LinearLayout > PreferenceFragment is zero height

查看:322
本文介绍了ScrollView>线性布局PreferenceFragment为零高度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have an activity that has a ScrollView with a vertical LinearLayout that has two fragments that are PreferenceFragment instances as shown in the following layout file:

<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中一样.

The problem is that the two fragments show up with just their PreferenceCategory title and the actual fragment UI is zero height and not visible. Oddly, it is possible to scroll each fragment individually and see the missing fragment UI. It is as if each Fragment is inside a ScrollView.

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

What I expected was for the two fragments to be sized to wrap their content and there be a single vertical slider to scroll the LinearLayout containing both fragments.

在相关的情况下,这两个片段扩展了android.preference.PreferenceFragment,并且未在布局文件中定义其布局.相反,他们按如下方式加载其首选项UI:

In case it is relevant, the two fragments extends android.preference.PreferenceFragment and do not define their layout in a layout file. Instead they load their Preference UI as follows:

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

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

TIA为您提供帮助.

TIA for your help.

推荐答案

这个问题已经很老了,但是如果有人偶然发现了这个问题,我已经设法找到了解决方案.就像@Ewoks提到的一样,我也必须设置一个固定的高度(在不同的设备dpi上并不总是能很好地发挥作用).

The question is quite old but in case someone else stumbles upon this, I've managed to find a solution. Just like @Ewoks mentioned, I had to settle for a fixed height as well (which didn't always play well with varying dpi of the devices).

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

But with the help of this code I managed to make the height dynamic, which wraps the content nicely.

在您的PreferenceFragment类中,请执行以下操作:

In your PreferenceFragment class do as follows:

@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);
        }
    }

}

希望这对某人有帮助!

这篇关于ScrollView&gt;线性布局PreferenceFragment为零高度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆