我正在使用Google I/O2011的iosched应用程序。看一下ScheduleFragment,我正在测试Workspace子进程来回翻动页面的不同视图。但是,让我的子视图填充父视图时遇到了问题。我添加了几种背景颜色和一些填充,以显示所有内容的布局。这是我的测试。
我在fragment_schedule.xml的“工作空间”项目中添加了一个红色的背景颜色,如下所示……
android:background="#cc0000"然后,我没有对子视图使用blocks_content.xml,而是创建了自己的pending_content.xml,它只是一个绿色背景的LinearLayout (用fill_parent表示高度)和一个蓝色背景的TextView (也用fill_parent表示高度),只有一个简单的文本行。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/container"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00cc00"
android:padding="5dip"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="@+id/text_view"
android:background="#0000cc"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Pending Content"
android:textColor="#ffffff"
/>
</LinearLayout>我添加我的视图,就像I/O应用有“天”一样……
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_schedule, null);
mWorkspace = (Workspace) root.findViewById(R.id.workspace);
ShootView view = new ShootView();
view.index = mViews.size();
view.rootView = (ViewGroup) inflater.inflate(R.layout.pending_content, null);
view.label = "Pending";
mWorkspace.addView(view.rootView);
mViews.add(view);这是我在设备上查看时看到的内容:

您将看到红色的工作区填充了可用的父空间,但绿色的LinearLayout没有。我遗漏了什么?
发布于 2011-06-07 21:07:17
我想出了一个解决方案。我开始在Workspace.java中调试onLayout。在child.layout函数调用中,我将child.getMeasuredHeight()更改为this.getMeasuredHeight(),以使用工作区的高度。
对于小于工作区高度的页面,以及大于工作区高度的子页面(长ListView),似乎都能很好地工作。
发布于 2011-06-04 02:23:06
问题是,在你的膨胀步骤中:
view.rootView = (ViewGroup) inflater.inflate(R.layout.pending_content, null);您丢失了XML语言中定义的LayoutParams,而视图实际上以默认的布局参数结束。
您需要做的是提供一个root,并且在膨胀期间简单地不要将膨胀的布局附加到根:
view.rootView = (ViewGroup) inflater.inflate(R.layout.pending_content, mWorkspace, false);这将允许布局膨胀器根据解析的LayoutParams属性为您的子视图构造一组合适的XML。
https://stackoverflow.com/questions/6229594
复制相似问题