3

我正在玩 Google I/O 2011 的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);

这是我在设备上查看时看到的内容:

http://www.milescrew.com/images/workspace.png

您会看到红色的 Workspace 填充了可用的父空间,但绿色的 LinearLayout 没有。我错过了什么?

4

2 回答 2

3

问题是在您的通货膨胀步骤中:

view.rootView = (ViewGroup) inflater.inflate(R.layout.pending_content, null);

您丢失了LayoutParamsXML 中的定义,您的视图实际上以默认布局参数结束。

您需要做的是提供一个root并且在膨胀期间根本不将膨胀的布局附加到根:

view.rootView = (ViewGroup) inflater.inflate(R.layout.pending_content, mWorkspace, false);

LayoutParams这将允许布局充气器根据解析的 XML 属性为您的子视图构造一组适当的集合。

于 2011-06-03T18:23:05.543 回答
0

我想出了一个解决办法。onLayout我开始在 Workspace.java 中调试。在 child.layout 函数调用中,我改为child.getMeasuredHeight()使用this.getMeasuredHeight()工作区的高度。

对于比工作区高度短的页面以及大于工作区高度的子(长 ListView)的情况,似乎工作正常。

于 2011-06-07T13:07:17.403 回答