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高度足以显示第一个元素(例如类别标题).
小智 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)
希望这有助于某人!
| 归档时间: |
|
| 查看次数: |
1778 次 |
| 最近记录: |